我正在尝试设置一个基本NSFetchedResultsController
,并且从下面的错误中向我抛出一个“null”错误。由于错误及其userInfo都为null,我不知道我甚至可以开始做什么来解决出错的问题。这是“让你的头撞到砖墙而不被告知为什么'编程时刻的人之一。这是造成崩溃的部分,位于viewDidLoad
:
NSError *error;
if (![_fetchedResultsController performFetch: &error]) {
NSLog(@"Error! %@, %@", error, [error userInfo]);
abort();
}
这是设置控制器的方法:
- (NSFetchedResultsController *) fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName: @"Quote" inManagedObjectContext: [self managedObjectContext]];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:nil ascending: YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setFetchBatchSize: 50];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest: fetchRequest managedObjectContext: [self managedObjectContext] sectionNameKeyPath: nil cacheName: @"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
我做错了什么?
答案 0 :(得分:0)
尝试替换
[_fetchedResultsController performFetch:&error]
而不是
[self.fetchedResultsController performFetch:&error]
如果未分配_fetchedResultsController
,则不会调度-fetchedResultsController
方法,只有您使用self.fetchedResultsController
,才会调度getter方法。