I am confused of the core data's logic.
I treated the core data as a database, and core data methods as a SQL query.
When I tried to update some object in a core data with a local memory's object, I found that I can make the feature just with below code:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Book" inManagedObjectContext:_managedObjectContext]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"identifier == %@", theBook.identifier];
[request setPredicate:predicate];
NSError *error;
Book *book = [[_managedObjectContext executeFetchRequest:request error:&error] lastObject];
if (error) {
NSLog(@"Error getting book to update: %@", error);
abort();
}
// NSLog(@"%d", book == theBook);
error = nil;
if (![_managedObjectContext save:&error]) {
NSLog(@"Error saving the book: %@", error);
abort();
}
theBook is the object which I want to update the core data's object with.
And I found that the log message says the two objects are same...
I fetched the request and didn't do anything but it works. Why does it work?
theBookis also a result of fetch. - glast