开发者

Core Data deleting problem when closing app completely

开发者 https://www.devze.com 2023-04-03 13:47 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号