核心数据迁移失败

我的应用程序最近在应用程序商店崩溃,因为我没有迁移数据。 所以我正在关注核心数据迁移的这个教程 ,但它似乎并没有工作:

1.我为Core Data创build了一个新的模型版本并将其设置为当前版本…

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

我已经设置了当前的模型版本。

任何帮助将不胜感激。

先谢谢你,

阿卜杜拉·沙菲克

根据@斯科特我必须撤消对我的来源的所有更改,只改变目的地

有人可以解释一下吗?

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

 /*! 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; } 

错误说:

“无法find源存储模型”;

它看起来像你也修改了以前的模型和哈希重新生成。 而且它们不匹配,并且由于无法find源存储的模型。

在添加新的Core Data模型版本时,XCode将创build另一个模型,并提示您为新版本提供Model名称。 所以现在你应该在XCode项目中有两个模型。 一旦你创build了新版本,你就需要select它并将其设置为应用程序使用的当前版本。 您在项目中保留旧版本的模型。

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

所以上面的错误表明你无意中对模型的原始版本进行了修改(也许除了对新版本的修改之外)。 但是,如果没有正确的型号版本,核心数据将无法打开文件,因此您需要恢复原始核心数据模型的副本。 如果你所做的只是添加一个新的实体到新的模型中,那么检查这个新的实体是否已经被无意地添加到旧的模型中,如果是这样的话,只需从模型的旧版本中删除它。