我在我的应用版本之间共享一个Core Data Controller类。
该代码适用于iPhone版本,但在iPad版本上停止了。
这是从RootVC调用的代码:
#pragma mark - NSFetchedResultsController
- (NSFetchedResultsController *)fetchedResultsController {
NSLog(@"%s", __FUNCTION__);
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
// Create the fetch request for the entity
NSLog(@"1");
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
// Set up the fetched results controller
NSLog(@"2");
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSLog(@"3");
[fetchRequest setEntity:entity];
NSLog(@"4");
// Set the batch size to a suitable number
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *titleSortDesc = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:titleSortDesc, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate,
// nil for section name key path means "no sections"
NSFetchedResultsController *aFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
return _fetchedResultsController;
}
日志条目1显示。日志条目2未显示。我反而得到了这个崩溃:
2012-11-19 08:37:26.402 I'm an iPad
2012-11-19 08:37:26.400 -[CoreDataController asyncLoadPersistentStores]
2012-11-19 08:37:26.403 -[CoreDataController loadiCloudStore:]
2012-11-19 08:37:26.404 moc is <NSManagedObjectContext: 0x1e03e4f0>
2012-11-19 08:37:26.421 -[CoreDataController iCloudStoreURL]
2012-11-19 08:37:26.454 -[CoreDataController folderForUbiquityToken:]
2012-11-19 08:37:26.460 -[RootViewController viewDidLoad]
2012-11-19 08:37:26.469 RVC moc is (null)
2012-11-19 08:37:26.470 -[RootViewController fetchedResultsController]
2012-11-19 08:37:26.472 1
2012-11-19 08:37:26.474 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Person''
*** First throw call stack:
我将托管对象上下文从App Delegate传递给RootVC,如下所示:
rootViewController.managedObjectContext = self.coreDataController.mainThreadContext;
我可以用一些帮助搞清楚这一点。我不知道它适用于iPhone和OSX的方式有何不同,而不适用于iPad ..相同的共享数据控制器。
更新:
这些是Phillip评论中提供的日志结果,下面是......
RVC is <RootViewController: 0x1f88d1a0>
-[CoreDataController loadiCloudStore:]
MOC is <NSManagedObjectContext: 0x1eda3630>
-[CoreDataController iCloudStoreURL]
-[RootViewController viewDidLoad]
-[CoreDataController folderForUbiquityToken:]
-[RootViewController fetchedResultsController]
1
SELF is <RootViewController: 0x1f8859d0>
答案 0 :(得分:2)
从评论....
在应用程序委托中记录rootViewController
地址,并在控制器自己的方法中记录self
的相应地址,这表明你有两个不同的RootViewController实例(不同的对象地址),你在那里重新设置moc并不是那个试图用它来生成fetchedResultsController的人。