Core Data deleting problem when closing app completely
Hi I do have a problem with my Core Data storage! I delete it the following way like I found it here an stack overflow:
NSFetchRequest * allFriends = [[NSFetchRequest alloc] init];
[allFriends setEntity:[NSEntityDescription entityForName:@"Friend" inManagedObjectContext:self.managedObjectContext]];
[allFriends setIncludesPropertyValues:NO]; //only fetch the managedObjectID
NSError * error = nil;
NSArray * friends = [self.managedObjectContext executeFetchRequest:allFriends error开发者_运维技巧:&error];
[allFriends release];
//error handling goes here
for (NSManagedObject * Friend in friends) {
[self.managedObjectContext deleteObject:Friend];
}
this seams to work perfect at runtime! my tableview (which I manage with NSFetchedResultsController) clears and all is fine it looks! Also when I hit the home button and start it back up it works.
BUT if I even close it from the multitasking list (so completely close it) and start it back up all is back in the tableView again!
could anybody help me out with this?
Your code is fine but you have forgotten to commit all changes (objects were removed) made to database. So, you should add following lines to your code and after reopening the app your db will not contain that objects:
NSError *error;
if (![self.managedObjectContext save:&error])
{
// Update to handle the
NSLog(@"Unresolved error %@", error);
exit(-1); // Fail
}
Because all changes are stored in memory, don't forget to save the managed object context after some important or critical changes were made. Before you commit your changes, the database/presistent-store file will be in the previously saved state.
Are you saving the managedObjectContext
at any point before quitting? Normally you would save the context when the application enters the background or terminates.
精彩评论