2010-07-23 47 views
1

我需要了解有关NSManagedObjectContext更新的内容。 我有一个UITplitView与RootView上的UITableViewController和详细信息视图上的UIViewController。当我在一排数据挖掘,我加载一些数据标签和一个UITextView在那里我可以更新场:NSManagedObjectContext:是否autoupdate?

- (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的储蓄行,冒险是只是暂时删除。如果您退出应用程序并重新打开该应用程序,该对象仍然存在。更新字段时不会发生这种情况。我想知道为什么,如果我还应该在textViewDidFinishEditing方法中保存moc。 预先感谢您。

回答

1

这是改变对象的属性和添加或删除对象图中的整个对象之间的区别。

在第一个块中,您将更改现有对象的属性,该对象会自动保存,除非您运行撤消。这是因为该对象已经存在于对象图中,并且不必更改其他对象来进行更改。

在第二个块中,您将删除整个对象,并可能通过更改对象之间的关系来更改对象图本身。直到隐式保存才会提交该更改,因为这可能会触发大量对象中的级联更改。

相关问题