Reputation: 1038
I Know that when you finish resetting an attribute of an entity, the system will do the update work automatically. However, I find that sometimes it will not update in time. In my Photo Album app, when I
finish resetting the name of a album, and then enter this album, the photos of this album are displayed as expected. However, when I tap one photo(which is also an NSManagedObject in database, with a relationship called album) to enter another ViewController for displaying this photo, the app crashes. But if I exit the app after finishing changing the album's name(the update code works normally) and then enter the app again, everything works well and no crash happens. So I think the reason for the crash is that system fails to update the relationship of photos and album in time(more exactly the name for the album connected to particular photos are not updated in time).
So I want to know if there is some method for updating the database manually?
Here is my updating code:
+ (void)changeAlbumNameWithName: (NSString *)newName withOldName: (NSString *)oldAlbumName inManagedObjectContext: (NSManagedObjectContext *)context
{
Album *album;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Album"];
request.sortDescriptors = nil;
request.predicate = [NSPredicate predicateWithFormat:@"name = %@", oldAlbumName];
NSError *error;
NSArray *matches = [context executeFetchRequest:request error:&error];
if ([matches count] == 1) {
album = matches[0];
album.name = newName;
}
}
Thanks in advance!
Upvotes: 1
Views: 2667
Reputation: 1038
I found the solution to this problem. After updating the name of the album, I call "saveToURL:forSaveOperation:completionHandler:", one method of UIManagedDocument. With this method, the Core Data database are updated properly.
Upvotes: 0
Reputation: 5346
You need to call the save:
method on the NSManagedObjectContext
to commit the changes you've made.
Upvotes: 4