NSFetchedResultsController に裏打ちされたテーブルビューがあります。
基礎となるコンテキストのオブジェクトが変更されるたびに、NSFetchedResultsController は自動的に新しい属性値をテーブル ビューに反映します。わーい。
私が気付いた 1 つの例外は、sectionNameKeyPath に使用される属性値の更新が自動的に反映されないことです。
sectionNameKeyPath に使用される属性値は NSFetchedResultsController にとって非常に基本的であるため、テーブルビューを再度実行して再読み込みする必要があると思いますか?
更新:フェッチリクエストの設定に使用されるコードはこちら
- (void)configureFetch {
CoreDataHelper *cdh = [(AppDelegate *)[[UIApplication sharedApplication] delegate] cdh];
NSFetchRequest *request =
[NSFetchRequest fetchRequestWithEntityName:@"Item"];
request.sortDescriptors =
[NSArray arrayWithObjects:
[NSSortDescriptor sortDescriptorWithKey:@"locationAtHome.storedIn"
ascending:YES],
[NSSortDescriptor sortDescriptorWithKey:@"name"
ascending:YES],
nil];
[request setFetchBatchSize:15];
self.frc =
[[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:cdh.context
sectionNameKeyPath:@"locationAtHome.storedIn"
cacheName:nil];
self.frc.delegate = self;
}
およびフェッチを実行するコード:
- (void)performFetch {
if (self.frc) {
[self.frc.managedObjectContext performBlockAndWait:^{
NSError *error;
[self.frc performFetch:&error];
if (error) NSLog(@"%@ '%@' %@ (Reason: %@)",
self.class, NSStringFromSelector(_cmd),
error.localizedDescription, error.localizedFailureReason);
[self.tableView reloadData];
}];
}
}