【问题标题】:Core Data and Restkit Simple Lightweight Migration ErrorCore Data 和 Restkit 简单轻量级迁移错误
【发布时间】: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


【解决方案1】:

Tom 是正确的 - 我的问题是:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多