ios - iPhone CoreData 迁移失败, "Can' t 为源存储找到模型”

标签 ios iphone cocoa-touch core-data migration

我有一个使用 CoreData 的 iPhone 应用程序。我最近对数据模型做了一些小改动,现在每次打开应用程序时都会收到错误消息“无法找到源商店的模型”。

我有 2 个版本的数据模型,我所做的唯一更改是添加了一些字段。我正在按照最初工作的指南 here 进行操作,然后就在今天,在添加了一些额外的字段之后,它中断了。所有附加字段都标记为可选,并且都具有默认值。迁移代码如下:

NSURL *storeUrl = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"xxx.sqlite"]];

// migration options
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
...
}

这里成功创建了managedObjectModel:

- (NSManagedObjectModel *)managedObjectModel {

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

NSString *path = [[NSBundle mainBundle] pathForResource:@"DataModelName" ofType:@"momd"];
NSURL *momURL = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];

return managedObjectModel;
}

我已将问题追查到 1 个实体的版本不匹配。抛出的错误包括该实体的错误:

MyEntityName = <cc1456b7 b12d0d05 21930308 94ccc078 27a6c345 8847c738 e3a9ae7e 0be9535d>;

但应用程序包中 VersionInfo.plist 中的哈希是:

MyEntityName = <fede6b59 462442d1 8fc98226 b9f8f745 3250dabd ee188248 cb97b1d0 8a74eef3>;

VersionInfo.plist 中的任何地方都没有其他实体具有散列 <cc1456b7....>

最佳答案

从链接的答案,

This seems great and as simple as I wanted - but I think you need to be careful during development as you change a model - otherwise you will have to create a new version for each change.

听起来您制作了版本 2,编辑了版本 2,运行了应用程序,再次编辑了版本 2,然后再次运行了应用程序。这不太管用;您需要保存所有 您希望能够打开的模型版本。这有点麻烦。

您可以做的是以应用程序版本命名所有模型,例如FooModel-1 和 FooModel-1.1 对应版本,FooModel-1.2d1、FooModel-1.2d2 对应“开发”版本。在发布之前,您可以将 FooModel-1.2d10 重命名为 FooModel-1.2 并删除其他开发版本。

(或者我可能完全误读了这个问题;抱歉。)

关于ios - iPhone CoreData 迁移失败, "Can' t 为源存储找到模型”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3791568/

相关文章:

ios - iPhone App "Content Aggregator"——担心应用商店被拒绝

iphone - 耳机/麦克风插孔的带宽

iphone - 创建 plist 文件 iOS 不工作

objective-c - 在 Cocoa Touch 中以编程方式将 View 位置设置为右上角

ios - 使用多行contentView时accessoryView的对齐方式

ios - 使用 currentUser 更新用户信息时解析错误

ios - SequenceType 与 DictionaryGenerator - 当 Class 工作正常时,Struct 出现问题?

ios - xcode 5.0.2 但没有 iOS 7.0.4?

ios - 将一个应用划分为多个具有不同UI设计的应用并共享逻辑代码

cocoa-touch - 单个 UiTableView 中的多个 UiTableViewCell 子类?为什么这段代码不起作用