persistentStoreCoordinator is nil ??
persistentStoreCoordinator is nil ??
persistentStoreCoordinator is nil ??
persistentStoreCoordinator is nil ??
persistentStoreCoordinator is nil ??
persistentStoreCoordinator is nil ??
persistentStoreCoordinator is nil ??
persistentStoreCoordinator is nil ??
persistentStoreCoordinator is nil ??
------------ persistentStoreCoordinator is 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) {
for (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 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;
}
最佳答案
我遇到了与您完全相同的撞车事故。最终,我发现无法将persistentStoreCoordinator
的初始化NSManagedObjectContext
放入后台线程中!
在您的情况下,您在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
}