Ios nil不是搜索实体名称的合法协调人';GroupMessageInfo';
错误消息:Ios nil不是搜索实体名称的合法协调人';GroupMessageInfo';,ios,objective-c,core-data,Ios,Objective C,Core Data,错误消息: 我和你的遭遇一模一样。最后我发现,NSManagedObjectContext的initializepersistentstorecordinator不能放置在后台线程中 在您的情况下,您在11号线程执行一些CoreData操作,该线程显然不是主线程。如果这是您第一次访问CoreData,那么persistentStoreCoordinator将设置在11号后台线程,从而导致崩溃。您还可以通过将这些代码appDidFinishLaunch放在AppDelegate - (
我和你的遭遇一模一样。最后我发现,
NSManagedObjectContext
的initializepersistentstorecordinator
不能放置在后台线程中
在您的情况下,您在11号线程执行一些CoreData操作,该线程显然不是主线程。如果这是您第一次访问CoreData
,那么persistentStoreCoordinator
将设置在11号后台线程,从而导致崩溃。您还可以通过将这些代码appDidFinishLaunch
放在AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// your CoreData operation here
}
// ...
}
解决方案非常简单,只需确保CoreData的初始化(或您第一次使用它时)应该在主线程上,否则,使用GCD切换到它
dispatch_async(dispatch_get_main_queue(), 0), ^{
// your CoreData initialize here
}
在从后台线程调用之前是否已创建堆栈?
+ (NSFetchRequest*)makeRequest:(NSManagedObjectContext*)ctx predicate:(NSPredicate*)predicate orderby:(NSArray*)orders offset:(int)offset limit:(int)limit {
NSString *className = [NSString stringWithUTF8String:object_getClassName(self)];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:className inManagedObjectContext:ctx]];
if (predicate) {
[fetchRequest setPredicate:predicate];
}
if (orders!=nil) {
[fetchRequest setSortDescriptors:orders];
}
if (offset>0) {
[fetchRequest setFetchOffset:offset];
}
if (limit>0) {
[fetchRequest setFetchLimit:limit];
}
return fetchRequest;}
- (NSManagedObjectContext*)createPrivateObjectContext {
if (_privateManagedObjectContext == nil) {
_privateManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_privateManagedObjectContext setParentContext:self.managedObjectContext];
}
return _privateManagedObjectContext;
}
- (NSManagedObjectContext *)managedObjectContext {
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel {
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"PokerSkyModel" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
......
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:optionsDictionary error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
[self cleanData];
abort();
}
return _persistentStoreCoordinator;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// your CoreData operation here
}
// ...
}
dispatch_async(dispatch_get_main_queue(), 0), ^{
// your CoreData initialize here
}