Core data ';找不到指定的持久存储;CoreData中的错误

Core data ';找不到指定的持久存储;CoreData中的错误,core-data,migration,datamodel,Core Data,Migration,Datamodel,我得到了这个错误: 该操作无法完成。(错误134140。) *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因是:“未找到指定的持久存储。” 我刚换了一个新版本的数据模型。但是,如果我激活了数据模型的早期版本,那么应用程序工作正常。如果我激活新版本,我会得到那个错误。我所做的唯一更改是,我将数据模型中的所有变量设置为非可选变量,这应该能够由自动迁移功能处理。我不理解当[fileManager copyItemAtPath:defaultStorePat

我得到了这个错误: 该操作无法完成。(错误134140。) *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因是:“未找到指定的持久存储。”

我刚换了一个新版本的数据模型。但是,如果我激活了数据模型的早期版本,那么应用程序工作正常。如果我激活新版本,我会得到那个错误。我所做的唯一更改是,我将数据模型中的所有变量设置为非可选变量,这应该能够由自动迁移功能处理。我不理解当[fileManager copyItemAtPath:defaultStorePath toPath:myPathDocs error:&error]没有引发错误时,为什么它会说找不到持久存储区

我似乎认为这与我的主捆绑包中的sqlite文件来自旧版本的数据模型有关,但没有来自新版本的sqlite文件。然而,我的印象是,自动迁移只需为新模型本身生成一个新文件(?),并将数据从现有文件迁移到其中。这是错的吗

在我的iOS应用程序中,我的资源中有一个名为Formats.sqlite的文件。在干净安装后启动时,应用程序会查看应用程序的/Documents文件夹中是否存在Formats.sqlite。如果没有,它会将主捆绑包中的一个复制到那里。在我制作我的模型的新版本之前,一切都很顺利。以下是我使用的代码:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    int count = [[persistentStoreCoordinator persistentStores] count];
    if (persistentStoreCoordinator != nil && count) {
        return persistentStoreCoordinator;
    }
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:@"Formats.sqlite"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    if (![fileManager fileExistsAtPath:myPathDocs]) {
        NSLog(@"SQLite file not found, copying SQLITE file");
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Formats" ofType:@"sqlite"];
        if (defaultStorePath) {
            if(![fileManager copyItemAtPath:defaultStorePath toPath:myPathDocs error:&error]) {
                NSLog(@"Error copying file at defaultStorePath to the documents path: %@",error);
            }
        }
    }
    storeURL = [NSURL fileURLWithPath:myPathDocs];
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
                             nil];
    if (persistentStoreCoordinator == nil) {
        persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    }
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
    {//my error code here}
然后错误被抛出到“我的错误代码在这里”的地方。我该怎么做才能修复这个问题?为什么会这样?顺便说一句,控制台列出了迁移的所有步骤,因此看起来它正在尝试这样做,但是它找不到持久存储,我不明白为什么它找不到持久存储,因为它已添加到构建目标中


我只是想确保,如果人们从以前版本的应用程序升级,他们的数据会被迁移,而不必重新输入所有内容。谢谢

为了找到错误的来源,我使用以下代码从调试器中获取错误的最详细描述:

    NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
    if(detailedErrors != nil && [detailedErrors count] > 0) {
        for(NSError* detailedError in detailedErrors) {
            NSLog(@"  DetailedError: %@", [detailedError userInfo]);
        }
    } else {
        NSLog(@"  %@", [error userInfo]);
    }
然后,在使用该代码之后,错误消息说我的模型中的一个属性没有默认值,自动迁移需要一个默认值才能正常工作。在我为该属性添加了默认值之后,一切都正常了


上面的代码片段对于调试核心数据错误非常有用,因为没有它,核心数据不一定会告诉您模型中的哪个确切属性是问题的根源。使用“detailedError”代码,我得到了所需的确切信息谢谢,“详细错误。”

谢谢。我有这个问题,我读了这个。它救了我。然后我意识到是我写的。啊哼。