我的应用程序最近在应用程序商店崩溃,因为我没有迁移数据。所以我在核心数据迁移上遵循这个tutorial,但它似乎没有起作用:
1.我为Core Data创建了一个新的模型版本,并将其设置为当前版本......
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";
}
我已经设置了当前的模型版本。
任何帮助将不胜感激。
提前谢谢你,
Abdullah Shafique
根据@Scott,我必须撤消对我的来源的所有更改,并且只更改目的地
有人可以解释一下吗?
答案 0 :(得分:2)
尝试使用此代码检查模型版本并输出元数据
/*! 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;
}
答案 1 :(得分:1)
错误说:
&#34;无法找到源存储的模型&#34;;
看起来您还修改了以前的模型并重新生成了哈希值。并且它们不匹配,因为无法找到源存储的模型。
答案 2 :(得分:1)
当您添加新的Core Data模型版本时,XCode会创建另一个模型,并提示您为新版本提供模型名称。所以现在你应该在XCode项目中有两个模型。创建新版本后,您需要选择它并将其设置为应用程序使用的当前版本。您将旧版本的模型保留在项目中。
为了让Core Data打开您现有的文件,它需要旧版本的模型(源存储模型)。然后,它将通过将数据移动到新架构(或模型)来执行迁移。
因此上述错误表明您无意中对模型的原始版本进行了更改(可能除了对新版本的更改之外)。但是,如果没有正确的模型版本,Core Data将无法打开该文件,因此您需要还原原始核心数据模型的副本。如果你所做的只是在新模型中添加一个新实体,那么检查这个新实体是否无意中被添加到旧模型中,如果是这样,只需将其从旧版本的模型中删除。