Alex Rivera | Logout

How to Perform a Insert/Update With Core Data

Asked 2010-11-04T23:52:29.423
26

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.

Edit
Report

1 Answer

38

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

answered 2010-11-05T00:09:15.793

Your Answer