我需要了解一些有关 NSManagedObjectContext 更新的信息。我有一个 UISplitView,在 RootView 上有一个 UITableViewController,在 Detail View 上有一个 UIViewController。当我用数据连续点击时,我将一些数据加载到标签和 UITextView 中,我可以在其中更新该字段:
- (void)textViewDidEndEditing:(UITextView *)textView {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
[[listOfAdventures objectAtIndex:indexPath.row] setAdventureDescription:textView.text];
}
行。这工作正常,描述已更新。此外,有人可能想要删除一行:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"playerPlaysAdventure.adventureName==%@",[[listOfAdventures objectAtIndex:indexPath.row] adventureName]];
NSArray *results = [[AdventureFetcher sharedInstance] fetchManagedObjectsForEntity:@"Player" withPredicate:predicate withDescriptor:@"playerName"];
[moc deleteObject:[listOfAdventures objectAtIndex:indexPath.row]];
for ( Player *player in results ) {
[moc deleteObject:player];
}
[listOfAdventures removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
[self clearDetailViewContent];
NSError *error = nil;
if ( ![moc save:&error] ) {
NSLog( @"Errore nella cancellazione del contesto!" );
abort();
}
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
所以这是我的问题:如果我评论有关保存我的 MOC 的行,冒险只是暂时删除。如果您退出应用程序并重新打开它,该对象仍然存在。更新字段时不会发生这种情况。我想知道为什么以及是否应该将 moc 也保存在 textViewDidFinishEditing 方法中。先感谢您。