How to Perform a Insert/Update With Core Data

36,926

There are two ways to insert data into Core Data - and whichever one you use is up to you. However, one of them depends on whether you have generated Model classes for your data model for the Core Data db.

The regular way is to use the following:

NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:@"table" 
inManagedObjectContext:context];
[object setValue:@"value1" forKey:@"stringColumn"];
[object setValue:12 forKey:@"numberValue"];
NSError *error;
if (![context save:&error]) {
    NSLog(@"Failed to save - error: %@", [error localizedDescription]);
}

This is assuming you've already got your managed object context set up. It is much more efficient if you create and insert your objects into the context in a loop, and then save after the loop ends.

The other method isn't much different, but is much safer in terms of type safety. If you have generated model classes (which you can do from the xcdatamodels) then you can simply create an object of that class and set its properties.

TableObject *object = [NSEntityDescription insertNewObjectForEntityForName:@"table" 
inManagedObjectContext:context];
[object setStringColumn:@"value1"];
[object setNumberValue:12];
NSError *error;
if (![context save:&error]) {
    NSLog(@"Failed to save - error: %@", [error localizedDescription]);
}

To delete from a table, simply retrieve the object from the table (I'm assuming you are using the second method here for insertions, and as such have generated model classes) and use the following:

[context deleteObject:object];

Note that you will need to call save for that to take effect as well.

Hope this helps! Good luck!

EDIT: Sorry, I must've misread the question!

To examine an existing record, you'll want to create a Fetch Request, and then execute it on your managed object context. At bare minimum, a Fetch Request requires an entity (so it knows what table to search on). To specify search terms, you will need to create a predicate (otherwise the request will simply return everything in the table). You can also specify a set of sort descriptors so that your results will be sorted.

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"table" inManagedObjectContext:context];
[request setEntity:entity];

NSError *errorFetch = nil;
NSArray *array = [context executeFetchRequest:request error:&errorFetch];

This code creates a fetch request, and returns every object from the table named "table" in an array. From here, since all of the required objects are in the array, you can inspect and edit the records. If you make any changes, remember to save the context! The following loop logs the first value in each object, using the same table as the above examples.

for(TableObject *object in array)
{
    NSLog(@"object value1 = %@", object.value1);
}

You can also delete records from this point as well using the above mentioned function.

For more information about Fetch Requests, please give the class reference a look. I would also highly recommend reading about sort descriptors and predicates, since they're very important for searching your Core Data db, and certain uses of them are less efficient than others (particularly in the creation of NSPredicates).

Good luck!

Share:
36,926

Related videos on Youtube

slevytam
Author by

slevytam

Updated on July 09, 2022

Comments

  • slevytam
    slevytam over 1 year

    I've got the basics of inserting records and deleting records with Core Data; however, I'd appreciate help with one of the most common functions - the insert/update.

    Basically, I use NSMutableArray arrayWithContentsOfURL to get an array that contains rows from a mysql table. What I need to do is now sync up my CoreData store.

    In other words, I need to add every row in the array to my CoreData table but if it already exists I need to update the record with the latest values. Also if it exists in Core Data and not in the downloaded array, I'd need to delete it.

    I probably could hack this together; however, I'd like to see how its properly and efficiently done without memory leaks.

    • Ben
      Ben over 11 years
      i know this is late, but if you're deleting items from the core data that don't exist in the array, why dont u just delete all the items from the core data and insert all the new ones. inserting vs updating ones that exist is probably a lot easier.
    • GeneralMike
      GeneralMike about 11 years
      @Ben: good suggestion, but wouldn't that be potentially much slower? Especially in the case of a table with many entries and only a couple that need to be changed. Doing that much writing when you don't need to could be significant, right?
    • Ben
      Ben about 11 years
      @GeneralMike yes definitely more efficient if you have lots of rows. I hadn't at the time learnt the proper way to do this :)
  • slevytam
    slevytam about 13 years
    Hi Andrew, thanks for the reply. I already understand how to insert records into Core Data using NSManagedObject as I mentioned in the question. I'm seeking help with the code to examine the records that already exist in Core Data then update them, delete them, or insert new ones based on the records in a NSMutableArray that ive downloaded.