CoreData“这NSPersistentStoreCoordinator没有持久性存储。 它

2019-06-27 05:58发布

我一直在以下这些说明来帮助整合与CoreData iCloud的支持,我得到了一些错误。

我有这个在我的AppDelegate:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }

    //NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Little_Wedding_Book_Universal.sqlite"];
    NSString *storePath = [[NSString stringWithFormat:@"%@", [self applicationDocumentsDirectory]] stringByAppendingPathComponent:@"appname.sqlite"];
    NSURL *storeURL = [NSURL fileURLWithPath:storePath];

    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    NSPersistentStoreCoordinator* psc = persistentStoreCoordinator;

    if (IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0"))
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSFileManager *fileManager = [NSFileManager defaultManager];

            // Migrate datamodel
            NSDictionary *options = nil;

            // this needs to match the entitlements and provisioning profile
            NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:@"J9VXW4WCE8.com.company.appname"];
            NSString* coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:@"data"];
            if ([coreDataCloudContent length] != 0) {
                // iCloud is available
                cloudURL = [NSURL fileURLWithPath:coreDataCloudContent];

                options = [NSDictionary dictionaryWithObjectsAndKeys:
                           [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                           [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                           @"appname.store", NSPersistentStoreUbiquitousContentNameKey,
                           cloudURL, NSPersistentStoreUbiquitousContentURLKey,
                           nil];
            }
            else
            {
                // iCloud is not available
                options = [NSDictionary dictionaryWithObjectsAndKeys:
                           [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                           [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                           nil];
            }

            NSError *error = nil;
            [psc lock];
            if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
            {
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
            }
            [psc unlock];

            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"asynchronously added persistent store!");
                [[NSNotificationCenter defaultCenter] postNotificationName:@"RefetchAllDatabaseData" object:self userInfo:nil];
            });

        });

    }
    else
    {
        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                                 nil];

        NSError *error = nil;
        if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
        {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }

    return persistentStoreCoordinator;
}

在另一个视图控制器创建我的一个实例AppDelegate ,创建一个对象,并调用[appDelegate saveContext]; 这是应用程序崩溃。 这一直完美地工作(到现在为止,加入iCloud的支持)。

-(void)saveContext
{
    NSError *error;
    if (managedObjectContext != nil) {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
            // Update to handle the error appropriately.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            exit(-1);  // Fail
        }
    }
}

所以它崩溃的这个方法,并在控制台给了我这样的信息:

'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores.  It cannot perform a save operation.'

我不知道该怎么办,求助!

编辑:控制台下面登录:

在启动应用程序时,我得到:

2012-08-22 17:39:47.906 appname[24351:707] asynchronously added persistent store!
2012-08-22 17:39:47.955 appname[24351:707] asynchronously added persistent store!

随访时,应用程序崩溃与:

2012-08-22 17:41:31.657 appname[24351:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores.  It cannot perform a save operation.'
*** First throw call stack:
(0x3749d88f 0x351a2259 0x36bf0fe7 0x36c59287 0xd648b 0x149e0b 0x373f73fd 0x3118ce07 0x3118cdc3 0x3118cda1 0x3118cb11 0x3118d449 0x3118b92b 0x3118b319 0x31171695 0x31170f3b 0x33bb322b 0x37471523 0x374714c5 0x37470313 0x373f34a5 0x373f336d 0x33bb2439 0x3119fcd5 0xd53dd 0xd5378)
terminate called throwing an exception(lldb) 

Answer 1:

你可能会调用persistentStoreCoordinator从两个(或更多)的方法不同的线程。 无论是直接或间接的影响。

作为编写的方法不是线程安全的。

这可能是你有其他的方法同样的问题。 通常managedObjectContext存在并且是写在一个类似的方式。

有两种方法来解决这个问题:

  • 使用@synchronize使这些方法是线程安全的
  • 移动初始化代码在他们外面并进入init方法和修改它们以断言/除非所述的ivar是nil

你也可能是做错了的第二件事是修改的背景下(例如添加新的管理对象的话),然后试图挽救它,店里被初始化之前。

为了解决这个问题,确保你做下列之一:

  • 你没有任何管理对象添加到上下文,除非你已知道至少有一个管理对象在它(意味着商店被加载)
  • 如果你需要添加一个管理对象的情况下,不检查是有一个已经(例如您最初创建空店后),请确保该商店已被初始化(例如做时,你得到的RefetchAllDatabaseData通知你注意数据库为空)。


Answer 2:

我有同样的问题,我的解决办法是删除的应用程序,并因为在数据库修改的重新安装。



Answer 3:

我解决它通过从模拟器的应用程序,并再次运行它。 我想这是因为应用程序的以前版本没有在它的核心数据。



Answer 4:

通过移动设MOC = DataController类()解决。managedObjectContext到类的顶部,而不是FUNC seedPerson的内部离开()



Answer 5:

我在测试过程中看到这个错误,当我打滑了一些临时NSManagedObjectContext •不用随时随地挂在他们-上下文将从下我随机由ARC得到释放出来。

什么清盘工作召开关于测试一个参照建设部和重置为每个测试。

不知道如何相关,这是对非测试情况,但特别是如果你使用的孩子/兄弟上下文,这是值得确保他们居然还在那里。 :P



Answer 6:

我有同样的问题。 我收到错误的原因是因为我做了我的CoreData实体模式 (基本上我加2-3个新的属性)一些小的改动 。 我忘记了它,因为它直到我们运行它不会给任何错误。 那么究竟是什么情况,这是前面已经运行的应用程序必须与之前的架构中的持久性存储。 因此,新的架构指向相同的商店。 这就是为什么我们恼火的错误,当我们运行它,因为我们试图访问新的属性。

解决方案删除设备/模拟器应用程序。 并重新安装 。 (新版本将有新的模式,因此不会崩溃。)



Answer 7:

更新重。 XCODE 4.6.3和MACOSX 10.9.5:

为了解决我删除在Xcode中得到的数据的位置与产品有关的应用程序目录中的问题:

RM -rd /用户//库/开发商/ Xcode中/ DerivedData /。 显然,数据获取与对象模型上场后搞砸了。



Answer 8:

我也坚持了这个问题。 对于我的第一次检查核心数据迁移代码由于代码是在我的情况下是正确的,我有一个从设备中删除我的应用程序,并重新安装。 和它的作品。



文章来源: CoreData 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.'