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