現在、FRCのfetchRequestのfetchLimitが4の場合、NSFetchedResultsControllerを使用するUITableViewController / UITableViewが約86個のアイテムを表示するという問題が発生しています。86個のアイテムがフェッチ自体を満たしていること、およびそれらが表示される理由を知っています。これは、didChangeObject:atIndexPath ...が86のそれぞれに対して呼び出され、デフォルトの実装の一種として挿入するためです。
私の質問は、fetchLimitがNSFetchedResultsControllerが「変更」(この場合は挿入)しようとするオブジェクトの数を制限しないのはなぜですか?
私のアプリケーションのユースケースは、最初のタブに、アプリの起動時に(サイドスレッドで)取得する一般的なフィードアイテムが表示されることです。それらを別のコンテキストのCoreDataに保存します。これは、最終的にメインスレッドのコンテキストにマージされ、変更された内容に対してFRCコールバックを開始します。私の質問は、特にアイテムが存在しない最初のケースに関するものです。
これが私のfetchRequestです:
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
fetchRequest.entity = [NSEntityDescription entityForName:ENTITY_CONTENT_ITEM inManagedObjectContext:managedObjectContext];
// Set a limit on the number of items returned
[fetchRequest setFetchLimit:4];
// Set a Predicate to limit the fetch to featured items only
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"featured == YES AND contentType == %d", contentType]];
// Set the sort descriptors
NSSortDescriptor *sortDateDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"sortDate" ascending:NO] autorelease];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDateDescriptor]];
上記のcontentTypeは、このタブに表示されるはずのものと他のタブに表示されるものを区別するための単なる方法です。アイテムのブールプロパティが注目されています。これは、表示用のオンオフスイッチのようなものです。
これが私のdidChangeObjectです:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[tableView cellForRowAtIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Reloading the section inserts a new row and ensures that titles are updated appropriately.
[tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
この質問に答えるのは難しいでしょうが、FRCがdidChangeObjectを呼び出す回数を決定する方法の説明でさえ本当に役に立ちます。