0

我正在尝试设置一个基本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;
}

我究竟做错了什么?

4

1 回答 1

0

尝试更换

[_fetchedResultsController performFetch:&error]

代替

[self.fetchedResultsController performFetch:&error]

因为如果_fetchedResultsController没有分配,您将不会调度该-fetchedResultsController方法,只有您使用self.fetchedResultsController,才会调度 getter 方法。

于 2013-02-25T12:40:08.810 回答