Thread1:EXC_BAD_ACCESS(代码= 2,地址= 0x7fff5450df68)

时间:2017-07-15 05:08:30

标签: ios core-data

我尝试从coredata中获取数据,并在使用NSFetchResultController的tableview中显示,但显示我这样的错误

  

线程1:EXC_BAD_ACCESS(代码= 2,地址= 0x7fff5450df68)目标c

- (NSFetchedResultsController *)fetchResultController {

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    _mainContext = [appDelegate manageObjectContext];

    if (_fetchResultController != nil) {
        return _fetchResultController;
    }

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:_mainContext];
    [request setEntity:entity];
    //[request setFetchBatchSize:20];
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"Student.name" ascending:NO];
    [request setSortDescriptors:[NSArray arrayWithObject:sort]];

    NSFetchedResultsController *theFetchResultController =[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:_mainContext sectionNameKeyPath:nil cacheName:@"Root"]; //this point show me error
    self.fetchResultController.delegate = theFetchResultController;

    return _fetchResultController;
}

请帮助解决此问题。

1 个答案:

答案 0 :(得分:2)

此问题通过此声明发生 编辑本

 self.fetchResultController.delegate = theFetchResultController;

替换它。

self.fetchResultController.delegate = self