Updating is as simple as creating a new one.
To update a specific object, you need to configure NSFetchRequest . This class is equivalent to the SQL SELECT statement.
Here is a simple example:
NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:[NSEntityDescription entityForName:@"Favorits" inManagedObjectContext:moc]]; NSError *error = nil; NSArray *results = [moc executeFetchRequest:request error:&error];
The results array contains all of the managed objects contained in the sqlite file. If you want to get a specific object (or several objects), you need to use a predicate with this query. For example:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title == %@", @"Some Title"]; [request setPredicate:predicate];
In this case, the results contain objects where the title is Some Title . Setting a predicate equals putting a WHERE clause in an SQL statement.
For more information, I suggest you read the Core Data programming guide and the NSFecthRequest class NSFecthRequest class.
Hope it helps.
EDIT (fragment that can be used for updating)
// maybe some check before, to be sure results is not empty Favorits* favoritsGrabbed = [results objectAtIndex:0]; favoritsGrabbed.title = @"My Title"; // save here the context
or if you are not using a subclass of NSManagedObject .
// maybe some check before, to be sure results is not empty NSManagedObject* favoritsGrabbed = [results objectAtIndex:0]; [favoritsGrabbed setValue:@"My title" forKey:@"title"]; // save here the context
In both cases, if you save context, the data will be updated.
Lorenzo B May 13, '12 at 13:28 2012-05-13 13:28
source share