如果我更改某些属性,是否需要手动为NSFetchedResultsController保存managedObjectContext?

Pie*_*ter 6 xcode objective-c nsfetchedresultscontroller ios

我正在使用NSFetchedResultsController来填充和管理我的表数据源.

当用户选择某一行时,会弹出一个操作表,然后让用户更改该行的值:

NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:selectedRowIndexPath];
[managedObject setValue:status forKey:@"status"];
Run Code Online (Sandbox Code Playgroud)

这非常有效,我可以在tableview中立即看到更改.这意味着NSFetchedResultsController知道某些内容已更改,因此重新加载该tableviewcell.当我停止并完全退出我的应用程序然后重新打开它时,更改不会保存.

我认为NSFetchedResultsConroller负责保存更改.

每次更改后,是否需要使用以下代码手动保存?

// Save the context.
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}
Run Code Online (Sandbox Code Playgroud)

或者可以将此代码调用:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
       atIndexPath:(NSIndexPath *)indexPath 
     forChangeType:(NSFetchedResultsChangeType)type 
      newIndexPath:(NSIndexPath *)newIndexPath
Run Code Online (Sandbox Code Playgroud)

Gob*_*bot 6

你是对的.您需要手动将上下文保存到商店.NSFetchedResultsController从上下文中获取数据,但不会将数据保存到存储中.