NSFetchedResultsController を使用して、日付を使用してセクション化された一連のオブジェクトを表示します。新規インストールでは、すべてが完全に機能し、オブジェクトがテーブル ビューに表示されます。ただし、アプリを再起動するとクラッシュするようです。NSFetchedResultsController を初期化するときにキャッシュを指定し、そうしないと完全に機能します。
NSFetchedResultsController を作成する方法は次のとおりです。
- (NSFetchedResultsController *)results {
// If we are not nil, stop here
if (results != nil)
return results;
// Create the fetch request, entity and sort descriptors
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"utc_start" ascending:YES];
NSArray *descriptors = [[NSArray alloc] initWithObjects:descriptor, nil];
// Set properties on the fetch
[fetch setEntity:entity];
[fetch setSortDescriptors:descriptors];
// Create a fresh fetched results controller
NSFetchedResultsController *fetched = [[NSFetchedResultsController alloc] initWithFetchRequest:fetch managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"day" cacheName:@"Events"];
fetched.delegate = self;
self.results = fetched;
// Release objects and return our controller
[fetched release];
[fetch release];
[descriptor release];
[descriptors release];
return results;
}
アプリがクラッシュしたときに表示されるメッセージは次のとおりです。
FATAL ERROR: The persistent cache of section information does not match the current configuration. You have illegally mutated the NSFetchedResultsController's fetch request, its predicate, or its sort descriptor without either disabling caching or using +deleteCacheWithName:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'FATAL ERROR: The persistent cache of section information does not match the current configuration. You have illegally mutated the NSFetchedResultsController's fetch request, its predicate, or its sort descriptor without either disabling caching or using +deleteCacheWithName:'
私はこれを引き起こすような特別なことをしているとは思わないので、なぜそれがそう言っているのかについては本当に手がかりがありません. 唯一の潜在的な問題は、新しいオブジェクトを作成するときに次のように作成するセクション ヘッダー (日) です。
// Set the new format
[formatter setDateFormat:@"dd MMMM"];
// Set the day of the event
[event setValue:[formatter stringFromDate:[event valueForKey:@"utc_start"]] forKey:@"day"];
前述したように、キャッシュが含まれていなければ、これはすべて正常に機能します。どんな助けでも大歓迎です!