【发布时间】:2014-01-02 18:03:39
【问题描述】:
我正在将 Restkit .20 集成到现有的 iOS 应用程序中,并且所有设置(看似)正确,并且可以使用我的初始模型。创建新对象、存储它们、检索它们等等,都在工作。在我使用 restkit/code 数据部署这个新版本的应用程序之前,我想测试一个简单的轻量级迁移,以确保未来的更改能够正常工作,但是当我这样做时,我总是得到“找不到源存储的模型”错误创建我的持久存储。这是我的 reskit 设置代码:
- (void)setupRestKit {
// set up the managed object and set the serialization type to json
_objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [self getServerBase]]]];
[_objectManager setRequestSerializationMIMEType:RKMIMETypeJSON];
// create our managed object models and store, and set the store on the manager
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
_objectManager.managedObjectStore = managedObjectStore;
<...all of my entity mapping and response descriptor code is here...>
// tell our store to create a persistent store coordinator
[managedObjectStore createPersistentStoreCoordinator];
// create the path to the database
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"<path>.sqlite"];
NSError *error;
// these options allow for lightweight migration
NSDictionary *options = @{
NSMigratePersistentStoresAutomaticallyOption: @(YES),
NSInferMappingModelAutomaticallyOption: @(YES)
};
// create our sqlite persistent store with this path and options
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:options error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
// tell our store to create the object contexts
[managedObjectStore createManagedObjectContexts];
// Configure a managed object cache to ensure we do not create duplicate objects
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
要创建我的数据模型的新版本,我选择了当前模型并转到“编辑器”->“添加模型版本”。复制原始模型后,我向我的一个对象添加了一个字符串属性“test”,通过选择新数据模型并点击 command-n 并选择 NSManagedObject 子类从我的模型生成新对象。然后我选择了新模型和所有对象并创建了它们。
在创建新模型、更改对象属性之一并基于此模型重新创建所有对象后,我在文件检查器中更改了模型版本以匹配我创建的新模型,然后运行项目 -我在哪里得到上述错误。任何人都知道/看到我做错了什么?
【问题讨论】:
-
您有多个数据模型吗?您使用的是
mergedModelFromBundles,而不是按名称询问模型,所以我想知道您是否真的在合并多个不同的模型文件。 -
我实际上是在几个小时前发现了这一点,而这正是问题所在。一旦我将其更改为获取正确模型的 url 路径并使用该路径初始化一个新模型,它似乎正在工作!
-
请将其添加为答案或删除问题以保持整洁:-)
标签: ios core-data restkit-0.20 core-data-migration