1

NSManagedObjectContextの更新について理解する必要があります。RootViewにUITableViewControllerがあり、詳細ビューにUIViewControllerがあるUISplitViewがあります。データのある行をタップすると、いくつかのデータがラベルとUITextViewに読み込まれ、そこでそのフィールドを更新できます。

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

Ok。これは正しく機能し、説明が更新されます。また、誰かが行を削除したいと思うかもしれません:

- (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を保存する理由と必要があるかどうかを知りたいのですが。前もって感謝します。

4

1 に答える 1

1

これは、オブジェクトの属性を変更することと、オブジェクトグラフでオブジェクト全体を追加または削除することの違いです。

最初のブロックでは、元に戻すを実行しない限り自動的に保存される既存のオブジェクトの属性を変更します。これは、オブジェクトがオブジェクトグラフにすでに存在し、変更を加えるために他のオブジェクトを変更する必要がないためです。

2番目のブロックでは、オブジェクト全体を削除し、オブジェクト間の関係を変更することでオブジェクトグラフ自体を変更する可能性があります。この変更は、暗黙的に保存されるまでコミットされません。これは、多数のオブジェクト全体で変更のカスケードをトリガーする可能性があるためです。

于 2010-07-23T15:38:04.993 に答える