2010-07-23 37 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

這是改變對象的屬性和添加或刪除對象圖中的整個對象之間的區別。

在第一個塊中,您將更改現有對象的屬性,該對象會自動保存,除非您運行撤消。這是因爲該對象已經存在於對象圖中,並且不必更改其他對象來進行更改。

在第二個塊中,您將刪除整個對象,並可能通過更改對象之間的關係來更改對象圖本身。直到隱式保存纔會提交該更改,因爲這可能會觸發大量對象中的級聯更改。