Ios 核心数据迁移失败

Ios 核心数据迁移失败,ios,core-data,crash,core-data-migration,Ios,Core Data,Crash,Core Data Migration,我的应用最近在应用商店崩溃,因为我没有迁移数据。因此,我在核心数据迁移方面遵循了这一点,但它似乎不起作用: 1.我为核心数据创建了一个新的模型版本,并将其设置为当前版本 2.我添加了以下代码: - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (_persistentStoreCoordinator != nil) { return _persistentStoreCoordinator;

我的应用最近在应用商店崩溃,因为我没有迁移数据。因此,我在核心数据迁移方面遵循了这一点,但它似乎不起作用:

1.我为核心数据创建了一个新的模型版本,并将其设置为当前版本

2.我添加了以下代码:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }
    
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Planner.sqlite"];
    
    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSDictionary *options = @{
                              NSMigratePersistentStoresAutomaticallyOption : @YES,
                              NSInferMappingModelAutomaticallyOption : @YES
                              };
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    
    
    return _persistentStoreCoordinator;
}
然而,当我在iPhone上运行更新应用程序时,它仍然崩溃

我所做的更改是我添加了一个实体

以下是崩溃日志:

Unresolved error Error Domain=NSCocoaErrorDomain Code=134130 "The operation couldn’t be completed. (Cocoa error 134130.)" UserInfo=0x14fb4530 {URL=file:///var/mobile/Applications/E8C39F0E-027E-4D5B-8D7B-74D592290D46/Documents/Planner.sqlite, metadata={
    NSPersistenceFrameworkVersion = 479;
    NSStoreModelVersionHashes =     {
        Audio = <f195c962 11c85401 0457370e e1b037e1 24c4e393 dc38ca34 cda3bb57 a3e26f2c>;
        Classes = <3b64e147 eb41441b 73bf051f ce094443 2017845b 2faba1c7 47848618 9fd9713b>;
        Homework = <4295bdbf 75862060 c7f1a501 3b0934cd 9811e838 bc40c5d5 05f8d383 f315d1f8>;
        Notes = <b7be1214 70a89b62 924df103 397a801a 96080505 be87b0e2 408ebf24 fdddb1a7>;
        Picture = <451cd1a2 9daac38d 69165176 0cacb6c8 94d1e93d eca34239 61a15f35 573f7e40>;
        Tests = <c10b5b25 228ebceb 6099b9af c830d203 bfca8e2b c9f46ee8 c0cd648e 9ad3e742>;
        Video = <b95e2033 b45b5aaf 298fef31 e6e25685 2b842e4e f2b3d9d1 82359c5f db9c78eb>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "4327E676-759B-4733-A7ED-12309BD482EE";
    "_NSAutoVacuumLevel" = 2;
}, reason=Can't find model for source store}, {
    URL = "file:///var/mobile/Applications/E8C39F0E-027E-4D5B-8D7B-74D592290D46/Documents/Planner.sqlite";
    metadata =     {
        NSPersistenceFrameworkVersion = 479;
        NSStoreModelVersionHashes =         {
            Audio = <f195c962 11c85401 0457370e e1b037e1 24c4e393 dc38ca34 cda3bb57 a3e26f2c>;
            Classes = <3b64e147 eb41441b 73bf051f ce094443 2017845b 2faba1c7 47848618 9fd9713b>;
            Homework = <4295bdbf 75862060 c7f1a501 3b0934cd 9811e838 bc40c5d5 05f8d383 f315d1f8>;
            Notes = <b7be1214 70a89b62 924df103 397a801a 96080505 be87b0e2 408ebf24 fdddb1a7>;
            Picture = <451cd1a2 9daac38d 69165176 0cacb6c8 94d1e93d eca34239 61a15f35 573f7e40>;
            Tests = <c10b5b25 228ebceb 6099b9af c830d203 bfca8e2b c9f46ee8 c0cd648e 9ad3e742>;
            Video = <b95e2033 b45b5aaf 298fef31 e6e25685 2b842e4e f2b3d9d1 82359c5f db9c78eb>;
        };
        NSStoreModelVersionHashesVersion = 3;
        NSStoreModelVersionIdentifiers =         (
            ""
        );
        NSStoreType = SQLite;
        NSStoreUUID = "4327E676-759B-4733-A7ED-12309BD482EE";
        "_NSAutoVacuumLevel" = 2;
    };
    reason = "Can't find model for source store";
}
Unresolved error Domain=nscocaerorrordomain code=134130“操作无法完成。(可可错误134130)。”UserInfo=0x14fb4530{URL=file:///var/mobile/Applications/E8C39F0E-027E-4D5B-8D7B-74D592290D46/Documents/Planner.sqlite,元数据={
NSPersistenceFrameworkVersion=479;
NSStoreModelVersionHashes={
音频=;
类别=;
家庭作业=;
附注=;
图片=;
测试=;
视频=;
};
NSStoreModelVersionHashesVersion=3;
NSStoreModelVersionIdentifiers=(
""
);
NSStoreType=SQLite;
NSStoreUUID=“4327E676-759B-4733-A7ED-12309BD482EE”;
“_NSAutoVacuumLevel”=2;
},reason=找不到源存储}的模型{
URL=”file:///var/mobile/Applications/E8C39F0E-027E-4D5B-8D7B-74D592290D46/Documents/Planner.sqlite";
元数据={
NSPersistenceFrameworkVersion=479;
NSStoreModelVersionHashes={
音频=;
类别=;
家庭作业=;
附注=;
图片=;
测试=;
视频=;
};
NSStoreModelVersionHashesVersion=3;
NSStoreModelVersionIdentifiers=(
""
);
NSStoreType=SQLite;
NSStoreUUID=“4327E676-759B-4733-A7ED-12309BD482EE”;
“_NSAutoVacuumLevel”=2;
};
reason=“无法找到源存储的模型”;
}
我已经设置了当前的模型版本

任何帮助都将不胜感激

提前谢谢大家,

阿卜杜拉·沙菲克

根据@Scott的说法,我必须撤销对源的所有更改,只更改目标

有人能解释一下吗?

错误是:

“找不到源存储的模型”

看起来您还修改了以前的模型,并重新生成了哈希。并且它们不匹配,并且由于无法找到源存储的模型。

错误显示:

“找不到源存储的模型”


看起来您还修改了以前的模型,并重新生成了哈希。并且它们不匹配,并且由于找不到源存储的模型。

当您添加新的核心数据模型版本时,XCode会创建另一个模型,并提示您提供新版本的模型名称。现在,在XCode项目中应该有两个模型。创建新版本后,需要选择它并将其设置为应用程序要使用的当前版本。在项目中保留模型的旧版本

为了让核心数据打开现有文件,它需要旧版本的模型(源存储模型)。然后,它将通过将数据移动到新模式(或模型)来执行迁移


因此,上面的错误表明您无意中对模型的原始版本进行了更改(可能除了对新版本的更改之外)。但是,如果没有正确的模型版本,Core Data无法打开文件,因此您需要恢复原始Core Data模型的副本。如果您所做的只是向新模型中添加一个新实体,那么请检查此新实体是否无意中添加到旧模型中,如果是这样,只需将其从旧版本的模型中删除即可。

当您添加新的核心数据模型版本时,XCode会创建另一个模型,并提示您为新版本提供模型名称。现在,在XCode项目中应该有两个模型。创建新版本后,需要选择它并将其设置为应用程序要使用的当前版本。在项目中保留模型的旧版本

为了让核心数据打开现有文件,它需要旧版本的模型(源存储模型)。然后,它将通过将数据移动到新模式(或模型)来执行迁移


因此,上面的错误表明您无意中对模型的原始版本进行了更改(可能除了对新版本的更改之外)。但是,如果没有正确的模型版本,Core Data无法打开文件,因此您需要恢复原始Core Data模型的副本。如果您所做的只是将新实体添加到新模型中,那么请检查此新实体是否无意中添加到旧模型中,如果是这样,请将其从旧模型版本中删除。

尝试使用此代码检查模型版本并输出元数据

/*! The method checks the Core Data file version is compatible with the App's model version
    and then pushes the main menu view onto the navigation stack.  If not compatible it displays a 
    message to the user.

    @param file The file URL for the Core Data Store. With UIManagedDocument you have to get the 
                actual store file URL, you can't just use the UIManagedDocument file URL.
 */
-(voidcheckCoreDataFileVersion:(NSURL*)file
{
    if ([self checkVersion:file]) {

        // file version is compatible so continue (add code to push the menu view)


    } else {

        // file version is NOT compatible

        _fileOpenErrorAlert = [[UIAlertView alloc] initWithTitle:@"Unable to open Document" message:@"Please check that you have the correct application version installed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [_fileOpenErrorAlert show];

    }
    return;
}
/*! Checks the Core Data files models version against the apps model version to see if they
    are compatible.  This will return YES if a lightweight migration can be performed and NO if NOT.

    @param fileURL The file URL for the Core Data Store. With UIManagedDocument you have to get the
                actual store file URL, you can't just use the UIManagedDocument file URL.
    @return  Returns YES if they are compatible and NO if not.
 */
- (bool)checkVersion:(NSURL*)fileURL {

    NSManagedObjectModel *model = [self managedObjectModel];

    NSLog(@" app model entity version hashes are %@", [model entityVersionHashesByName]);

    NSError *error;
    NSDictionary *metaData = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:fileURL error:&error];

    if (!metaData) {
        NSLog(@“ problem getting metaData");
        NSLog(@“  - error is %@, %@", error, error.userInfo);
        return NO;
    }

    bool result = [model isConfiguration:nil compatibleWithStoreMetadata:metaData];
    if (!result) {
       NSLog(@“ file is not compatible!");
       NSLog(@“ metadata is %@", metaData);
    }

    return result;

}

尝试使用此代码检查模型版本并输出元数据

/*! The method checks the Core Data file version is compatible with the App's model version
    and then pushes the main menu view onto the navigation stack.  If not compatible it displays a 
    message to the user.

    @param file The file URL for the Core Data Store. With UIManagedDocument you have to get the 
                actual store file URL, you can't just use the UIManagedDocument file URL.
 */
-(voidcheckCoreDataFileVersion:(NSURL*)file
{
    if ([self checkVersion:file]) {

        // file version is compatible so continue (add code to push the menu view)


    } else {

        // file version is NOT compatible

        _fileOpenErrorAlert = [[UIAlertView alloc] initWithTitle:@"Unable to open Document" message:@"Please check that you have the correct application version installed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [_fileOpenErrorAlert show];

    }
    return;
}
/*! Checks the Core Data files models version against the apps model version to see if they
    are compatible.  This will return YES if a lightweight migration can be performed and NO if NOT.

    @param fileURL The file URL for the Core Data Store. With UIManagedDocument you have to get the
                actual store file URL, you can't just use the UIManagedDocument file URL.
    @return  Returns YES if they are compatible and NO if not.
 */
- (bool)checkVersion:(NSURL*)fileURL {

    NSManagedObjectModel *model = [self managedObjectModel];

    NSLog(@" app model entity version hashes are %@", [model entityVersionHashesByName]);

    NSError *error;
    NSDictionary *metaData = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:fileURL error:&error];

    if (!metaData) {
        NSLog(@“ problem getting metaData");
        NSLog(@“  - error is %@, %@", error, error.userInfo);
        return NO;
    }

    bool result = [model isConfiguration:nil compatibleWithStoreMetadata:metaData];
    if (!result) {
       NSLog(@“ file is not compatible!");
       NSLog(@“ metadata is %@", metaData);
    }

    return result;

}

轻量级迁移只能处理某些类型的更改。您对模型做了哪些更改,出现了哪些错误?@DuncanGroenewald我添加了一个entityCould发布崩溃日志?@flexaddicted查看我的编辑…看看这里,轻量级迁移只能处理某些类型的更改。您对模型做了哪些更改,出现了哪些错误?@DuncanGroenewald我添加了一个entityCould发布崩溃日志?@flexaddicted查看我的编辑…看看这里我如何修复它?@AbdullahShafique:撤消对源的所有更改,只更改目标(您的新模型版本)@斯科特,你能解释一下怎么做吗?感谢您的帮助。@AbdullahShafique:我希望您正在使用SCM工具(git、svn等),然后您可以重置(或签出)以前的核心数据模型文件(更改之前的模型文件版本)。然后从头开始,或者解决当前的问题。因为可能在你改变之前,你