ios - nil is not a legal NSPersistentStoreCoordinator for searching for entity name 'GroupMessageInfo' -


error message:

enter image description here

persistentstorecoordinator nil ?? persistentstorecoordinator nil ?? persistentstorecoordinator nil ?? persistentstorecoordinator nil ?? persistentstorecoordinator nil ?? persistentstorecoordinator nil ?? persistentstorecoordinator nil ?? persistentstorecoordinator nil ?? persistentstorecoordinator nil ?? ------------ persistentstorecoordinator nil ?? 

- (void)updategrouptablestatus:(tmpgroupmessageinfo*)info {   nsmanagedobjectcontext *ctx = [self createprivateobjectcontext];  [ctx performblock:^{     nspredicate *cdt = [nspredicate predicatewithformat:@"groupid == %@ && tableid == %@", info.groupid, info.tableid];      nsfetchrequest *fetchrequest = [groupmessageinfo makerequest:ctx predicate:cdt orderby:nil offset:0 limit:1];     nsarray *array = [ctx executefetchrequest:fetchrequest error:nil];     if ([array count] > 0) {         (groupmessageinfo *msg in array) {             msg.lastingtime = [nsnumber numberwithlonglong:0];         }         [ctx save:nil];         [self.managedobjectcontext performblock:^{             [self.managedobjectcontext save:nil];         }];     } }]; } 

+ (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 managed object model application. // if model doesn't exist, created 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 persistent store coordinator application. // if coordinator doesn't exist, created , application's store added 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; } 

i came cross same crash yours. found out initialize persistentstorecoordinator of nsmanagedobjectcontext cannot placed in background thread!

in case, coredata operation @ no.11 thread not main thread.if first time access coredata, persistentstorecoordinator set @ no.11 background thread lead crash. can reproduce crash putting these code appdidfinishlaunch @ appdelegate

 - (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions {     dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{          // coredata operation here     }     // ... } 

the solution quite easy, ensure initialization of coredata(or first time use it) should @ main thread.when it's not, using gcd switch it

dispatch_async(dispatch_get_main_queue(), 0), ^{      // coredata initialize here } 

Comments