核心数据中的空FetchedResultsController

时间:2010-10-10 23:37:00

标签: iphone core-data

我正在使用核心数据,我有一个名为LogRecord的实体。在其他地方,使用以下对象填充:

        LogRecord *rec = [NSEntityDescription insertNewObjectForEntityForName:@"LogRecord"
                                                       inManagedObjectContext:managedObjectContext];
        [rec timestampNow];

        rec.moodType = [NSNumber numberWithUnsignedInteger:i];
        rec.moodValue = value;

        NSError *error = nil;
        if (![rec.managedObjectContext save:&error]) {
            [DataUtil displayFatalError:error message:@"Failed to save log entry"];
            errors++;
        }           

我已经检查过,使用此代码后底层的sqlite数据库中有记录。

但是,当我尝试使用以下方法查询数据时:

 - (NSFetchedResultsController *)fetchedResultsController {
    if (fetchedResultsController == nil) {

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"LogRecord"
                                              inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]    initWithKey:@"timestamp" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController =
        [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                            managedObjectContext:managedObjectContext
                                              sectionNameKeyPath:nil
                                                       cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
        [sortDescriptors release];
    }

    return fetchedResultsController;
}    

它显示为空,如[[self.fetchedResultsController sections] count]返回零所示。

知道如何调试这个吗?在我尝试将此代码分解为不同的UIViewController之前,它正在工作....

1 个答案:

答案 0 :(得分:7)

没关系....不知何故,viewDidLoad中对[[self fetchedResultsController] performFetch:&error]的调用被删除了。