2010-07-23 5 views
1

NSManagedObjectContext 업데이트에 대해 이해해야합니다. RootView에는 UITableViewController가 있고 Detail View에는 UIViewController가있는 UISplitView가 있습니다.NSManagedObjectContext : 자동 업데이트 또는 안 함?

- (void)textViewDidEndEditing:(UITextView *)textView { 
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
[[listOfAdventures objectAtIndex:indexPath.row] setAdventureDescription:textView.text]; 
} 

확인 : 나는 데이터 행에 탭 때, 나는 라벨로 일부 데이터와 나는 그 필드를 업데이트 할 수있는 UITextView를로드합니다. 이것은 올바르게 작동하며 설명이 업데이트됩니다. 또한 는, 누군가가 수도 행을 삭제하려고 :

- (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 메서드에서도 저장해야하는지 알고 싶습니다. 미리 감사드립니다.

답변

1

개체의 속성을 변경하고 개체 그래프에서 전체 개체를 추가하거나 제거하는 것의 차이점은 다음과 같습니다.

첫 번째 블록에서 실행 취소를 실행하지 않는 한 자동으로 저장되는 기존 객체의 속성을 변경합니다. 이는 오브젝트 그래프에 오브젝트가 이미 존재하고 변경하기 위해 다른 오브젝트를 변경하지 않아도되기 때문입니다.

두 번째 블록에서는 전체 개체를 제거하고 개체 간 관계를 변경하여 개체 그래프 자체를 변경하는 중입니다. 이 변경은 암시 적 저장까지 커밋되지 않습니다. 잠재적으로 많은 수의 개체에 변경 사항이 적용될 수 있기 때문입니다.

관련 문제