When I insert new object I do with following code:
NSManagedObjectContext *context = [appDelegate managedObjectContext];
Favorits *favorits = [NSEntityDescription insertNewObjectForEntityForName:@"Favorits" inManagedObjectContext:context];
favorits.title = @"Some title";
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops");
}
How can I update existing object in core data?
Updating is simple as creating a new one.
To update a specific object you need to set up a
NSFetchRequest. This class is equivalent to a SELECT statetement in SQL language.Here a simple example:
The array
resultscontains 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. For example:In this case
resultscontains the objects where title is equal toSome Title. Setting a predicate is equal to put the WHERE clause in a SQL statement.For further info I suggest you to read the Core Data programming guide and
NSFecthRequestclass reference.Core Data Programming Guide
NSFecthRequest Class Reference
Hope it helps.
EDIT (snippet that can be used to update)
or if you are not using a
NSManagedObjectsubclass.In both cases if you do a
saveon the context, data will be updated.