为已发布的iPhone应用程序迁移Realm数据库的步骤

时间:2016-01-31 23:40:35

标签: ios objective-c realm data-migration

为已发布的更新iOS应用迁移Realm DB更改的步骤是什么?

在运送Realm.io数据库应用程序之前,是否应该先执行任何步骤?

以下是关于核心数据的类似问题 Steps to migrate Core Data databases for shipped iPhone apps,但我正在寻找迁移Realm数据库。

以下是崩溃日志:

  

***由于未捕获的异常终止应用' RLMException',原因:'对象类型需要迁移' ExampleRealm'因为   以下错误:    - 财产价值'已被添加到最新的对象模型。'

1 个答案:

答案 0 :(得分:3)

根据the Realm documentation,有一些关于如何在Realm中进行迁移的示例。

从示例代码中可以看出:

// define a migration block
// you can define this inline, but we will reuse this to migrate realm files from multiple versions
// to the most current version of our data model
RLMMigrationBlock migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
    if (oldSchemaVersion < 1) {
        [migration enumerateObjects:Person.className block:^(RLMObject *oldObject, RLMObject *newObject) {
            if (oldSchemaVersion < 1) {
                // combine name fields into a single field
                newObject[@"fullName"] = [NSString stringWithFormat:@"%@ %@", oldObject[@"firstName"], oldObject[@"lastName"]];
            }
        }];
    }
    if (oldSchemaVersion < 2) {
        [migration enumerateObjects:Person.className block:^(RLMObject *oldObject, RLMObject *newObject) {
            // give JP a dog
            if ([newObject[@"fullName"] isEqualToString:@"JP McDonald"]) {
                Pet *jpsDog = [[Pet alloc] initWithValue:@[@"Jimbo", @(AnimalTypeDog)]];
                [newObject[@"pets"] addObject:jpsDog];
            }
        }];
    }
    if (oldSchemaVersion < 3) {
        [migration enumerateObjects:Pet.className block:^(RLMObject *oldObject, RLMObject *newObject) {
            // convert type string to type enum if we have outdated Pet object
            if (oldObject && oldObject.objectSchema[@"type"].type == RLMPropertyTypeString) {
                newObject[@"type"] = @([Pet animalTypeForString:oldObject[@"type"]]);
            }
        }];
    }
    NSLog(@"Migration complete.");

看起来您声明了一个块,您可以在其中枚举对象并手动更新架构。