gpt4 book ai didi

ios - nil 不是用于搜索实体名称 'GroupMessageInfo' 的合法 NSPersistentStoreCoordinator

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:32:44 26 4
gpt4 key购买 nike

错误信息:

enter image description here

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;
}

最佳答案

我遇到了与您完全相同的崩溃。最后我发现初始化NSManagedObjectContextpersistentStoreCoordinator不能放在后台线程!

在你的例子中,你在 11 号线程做了一些 CoreData 操作,这显然不是Main Thread。如果这是你第一次访问你的 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的初始化(或者第一次使用)在Main Thread即可,如果不是,使用GCD切换到它即可

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

关于ios - nil 不是用于搜索实体名称 'GroupMessageInfo' 的合法 NSPersistentStoreCoordinator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38698235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com