Iphone 完全关闭应用程序时核心数据删除问题

Iphone 完全关闭应用程序时核心数据删除问题,iphone,objective-c,core-data,nsfetchedresultscontroller,Iphone,Objective C,Core Data,Nsfetchedresultscontroller,嗨,我的核心数据存储确实有问题! 我按以下方式删除它,就像我在这里发现它是堆栈溢出一样: NSFetchRequest * allFriends = [[NSFetchRequest alloc] init]; [allFriends setEntity:[NSEntityDescription entityForName:@"Friend" inManagedObjectContext:self.managedObjectContext]]; [allFriends setIncludesP

嗨,我的核心数据存储确实有问题! 我按以下方式删除它,就像我在这里发现它是堆栈溢出一样:

 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];
}
这将在运行时完美工作! 我的tableview(我使用NSFetchedResultsController管理)已清除,看起来一切正常! 而且,当我按下home(主页)按钮并启动它时,它就会工作

但是,如果我甚至从多任务列表中关闭它(完全关闭它)并启动它,那么所有这些都会再次回到tableView中


有人能帮我解决这个问题吗?

您是否在退出之前随时保存
managedObjectContext
?通常,当应用程序进入后台或终止时,您会保存上下文。

您的代码很好,但您忘记了提交对数据库所做的所有更改(对象已删除)。因此,您应该在代码中添加以下行,并且在重新打开应用程序后,您的db将不包含该对象:

    NSError *error;
    if (![self.managedObjectContext save:&error]) 
    {
        // Update to handle the 
        NSLog(@"Unresolved error %@", error);
        exit(-1);  // Fail
    }

因为所有更改都存储在内存中,所以在进行一些重要或关键更改后,不要忘记保存托管对象上下文。在提交更改之前,数据库/现有存储文件将处于以前保存的状态。

非常感谢您,我不知道这一点!工作起来很有魅力!很好的解释,谢谢。