核心数据 - 创建NSFetchedResultsController时运行时崩溃

时间:2013-03-29 20:55:10

标签: ios objective-c core-data runtime-error nsfetchedresultscontroller

我正在使用的TableViewController类与在Xcode中启动新的Master-Detail Application项目时创建的类非常相似。因此,我使用的是在TableViewController类中预先填充的相同代码供我自己使用。但是,我遇到了运行时崩溃,我不知道为什么。我在我的应用程序的另一个类中使用了这个确切的代码,它运行得很好。

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Binder" inManagedObjectContext:[appDelegate managedObjectContext]];
    [fetchRequest setEntity:entity];

    // Edit the sort key as appropriate.
    //NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    //NSArray *sortDescriptors = @[sortDescriptor];

    //[fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".

//This is where it crashes
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[appDelegate managedObjectContext] sectionNameKeyPath:nil cacheName:@"Master"];
//End crash
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}

我不确定此处包含哪些其他代码段。崩溃发生时输出没有告诉我什么,Xcode跳转到主线程的这一部分:

libsystem_kernel.dylib`__kill:
0x972893b0:  movl   $786469, %eax
0x972893b5:  calll  0x9728b4c2                ; _sysenter_trap
0x972893ba:  jae    0x972893ca                ; __kill + 26 //This is highlighted
0x972893bc:  calll  0x972893c1                ; __kill + 17
0x972893c1:  popl   %edx
0x972893c2:  movl   27739(%edx), %edx
0x972893c8:  jmpl   *%edx
0x972893ca:  ret    
0x972893cb:  nop  

有什么想法? 感谢

3 个答案:

答案 0 :(得分:4)

感谢@flashfabrixx,问题是我没有使用排序描述符,并且在使用NSFetchedResultsController时需要它们。一旦我重新添加排序描述符,一切都很完美。

答案 1 :(得分:0)

嗯,您正在做的唯一非标准事情是您正在使用来自app delegate的托管对象上下文。出于很多好的理由,我们不建议这样做。

尝试通过向主控制器添加上下文属性并使用该上下文创建获取的结果控制器(用于获取对实体的引用和FRC创建)来更改此项。

最后,确保您的模型确实包含有效的Binder实体。

答案 2 :(得分:0)

nil托管对象上下文传递给initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:将引发异常。我很惊讶你在控制台日志中没有看到任何东西。

尝试NSAssert()验证您的MOC和Binder实体都是非零的。

如果NSFetchedResultsController中的缓存名称已用于另一个FRC,除非两个控制器的提取请求相同,否则您将看到错误。设置nil(或不同的)cacheName:,看看是否得到了不同的结果。

相关问题