Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Perform a Insert/Update With Core Data

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.

like image 407
slevytam Avatar asked Nov 04 '10 23:11

slevytam


People also ask

How do you update an object in core data?

To update a specific object you need to set up a NSFetchRequest . This class is equivalent to a SELECT statetement in SQL language. The array results contains all the managed objects contained within the sqlite file. If you want to grab a specific object (or more objects) you need to use a predicate with that request.

How do I create a core data model?

Add a Core Data Model to an Existing ProjectChoose File > New > File and select the iOS platform tab. Scroll down to the Core Data section, select Data Model, and click Next. Name your model file, select its group and targets, and click Create.


1 Answers

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!

like image 132
Andrew Natoli Avatar answered Oct 07 '22 03:10

Andrew Natoli