Tutorial :How to update an item in core data



Question:

I am using Core Data. I retrieve data in a NSMutable array using some NSPredicate. Now I want to update some items. Let say I have a name string or some BOOL which I want to update. So how to do that. Like Is there any way I can update with respect to ID or something, becoz I dont get all the items from the managedObjectContext.


Solution:1

You assign the data to the objects. and then call

[managedObjectContext save:&error];  

on each the respective context object. You can call

[managedObject managedObjectContext];  

to get that items context

To get a managed object with a certain id you use NSFetchRequest and it's setPredicate method. then something like this

[request setEntity:[NSEntityDescription entityForName:@"Shirt" inManagedObjectContext:moc]];  NSArray *shirtsInDB = [moc executeFetchRequest:request error:nil];  

or this method in NSManagedObjectContext

- (NSManagedObject *)objectWithID:(NSManagedObjectID *)objectID  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »