核心数据和Restkit简单轻量级迁移错误

时间:2014-01-02 18:03:40

标签: ios core-data restkit-0.20 core-data-migration

我将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];

为了创建我的数据模型的新版本,我选择了当前模型并转到&#34;编辑器&#34; - &GT; &#34;添加模型版本&#34;。复制原始模型后,我添加了一个字符串属性&#34; test&#34;通过选择新的数据模型并命中command -n并选择NSManagedObject子类,从我的模型中生成新对象。然后我选择了新模型和所有对象并创建了它们。

创建新模型,更改其中一个对象属性,并根据此模型重新创建所有对象后,我在File Inspector中更改了模型版本以匹配我创建的新模型,并运行项目 - 我得到了上述错误。任何人都知道/看到我做错了什么?

1 个答案:

答案 0 :(得分:1)

汤姆是对的 - 我的问题是:

NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];

正如我所说,我的项目非常庞大,所以我没有意识到其中已有数据模型,所以我在设置Restkit时创建的托管对象模型试图合并所有他们在捆绑中一起,显然是行不通的。我的解决方案是更改代码,以便我只使用我用于Restkit的特定模型创建托管对象模型,如下所示:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Model" ofType:@"momd"];
NSURL *url = [NSURL fileURLWithPath:path];

NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:url];

现在,如果我想对模型进行更改,请单击我当前使用的模型的最新版本,转到编辑器 - &gt;添加模型版本并将新模型基于先前模型。在对模型中的对象进行更改之后,我将最新模型设置为文件检查器中“模型版本”下的“当前”模型,当我运行项目时,它工作得很好!