Core data NSFetchedResultsController存在问题

Core data NSFetchedResultsController存在问题,core-data,nsfetchedresultscontroller,Core Data,Nsfetchedresultscontroller,我的取回有一个大问题,真的不知道我的错误在哪里 问题出在ViewWillAppease函数中,我将我的东西加载到tableView中,代码如下: NSNotificationCenter * theCenter = [NSNotificationCenter defaultCenter]; NSFetchedResultsController *theController = [[NSFetchedResultsController alloc] initWithFetchRequest:se

我的取回有一个大问题,真的不知道我的错误在哪里

问题出在ViewWillAppease函数中,我将我的东西加载到tableView中,代码如下:

NSNotificationCenter * theCenter = [NSNotificationCenter defaultCenter];

NSFetchedResultsController *theController = [[NSFetchedResultsController alloc] initWithFetchRequest:self.fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];

NSError *theError = nil;

theController.delegate = self;
//everything fine
self.fetchedResultsController = theController;
//program stops
if(![self.fetchedResultsController performFetch:&theError]) {
    NSLog(@"viewDidLoad: %@", theError);
}

[theCenter addObserver:self
              selector:@selector(managedObjectContextDidSave:)
                  name:NSManagedObjectContextDidSaveNotification
                object:nil];
[tableView reloadData];
fetchedResultController有一些问题,但我真的不知道是什么类型的。 错误消息是:

  2013-10-22 10:53:04.750 myAppName[508:c07] CoreData: error: Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  *** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0] with userInfo (null)
编辑:NSFetcherResultsControllerDelegate:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)inController { 
     [tableView endUpdates]; 
} 

你为什么不使用苹果的核心数据模板呢?这将惰性地创建获取结果控制器,这是处理数据获取的首选方法。当您通过objectAtIndexPath等重新加载表时,会隐式调用它

要从模板中获取FRC,请创建新的项目主控详图,并从主控视图控制器的底部进行复制


我相信您的错误是由于多次调用fetched results controller属性造成的。您将注意到代码的细微差别:本地创建的FRC被分配给实例变量_fetchedResultsController,而整个方法是self.fetchedResultsController的getter

由于异常是指NSFetchedResultsConroller的委托方法,因此最好将该方法的实现添加到问题中。-voidcontrollerDidChangeContent:NSFetchedResultsController*InControl{[tableView EndUpdate];}我已经从主详细项目中获得了完整的委托。本地实例获取是正确的,但错误发生在self.fetchedresultscocontroller=theController中;我真的不知道为什么。为什么清楚?我在我的主类中有我自己的方法,而不是在委托中。所以你没有FRC方法。你为什么不试试那个而不是你的呢?