迁移,但仍然在RLMException'上崩溃,原因:'需要迁移

时间:2015-12-12 17:34:34

标签: ios objective-c realm realm-migration

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Realm, 1th thing
    {
        RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
        config.schemaVersion = 2;
        config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {

        };
        config.objectClasses = @[[User class], [UsersMenuItem class]];
        [RLMRealm migrateRealm:config];
    }

    ...
}

我确实在用户对象中添加了一个属性,文档说新域应该自动迁移,但是我崩溃了

*** Terminating app due to uncaught exception 'RLMException', reason: 'Migration is required for object type 'User' due to the following errors:
- Property 'realtedMenuItems' has been added to latest object model.'
*** First throw call stack:
(0x1838ad900 0x182f1bf80 0x10015db3c 0x10014aa60 0x100149a70 0x100116500 0x1000a6488 0x1000f1664 0x1885a00c0 0x18859fcc4 0x100039568 0x188615704 0x188844130 0x1888484b8 0x1888455c0 0x184e63790 0x184e63b10 0x183864efc 0x183864990 0x183862690 0x183791680 0x18860e580 0x188608d90 0x1000b7430 0x1833328b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

版本:0.95

注意:当我更新到0.96时,我得到

*** Terminating app due to uncaught exception 'RLMException', reason: 'Provided schema version 0 is less than last set version 3.'
*** First throw call stack:

2 个答案:

答案 0 :(得分:1)

看起来像添加

[RLMRealmConfiguration setDefaultConfiguration:config];

解决了这个问题,虽然不确定原因

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Realm
    {
        RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
        config.schemaVersion = 4;
        config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {

        };
        NSError * error = [RLMRealm migrateRealm:config];
        if (error) {
            NSLog(@"Error migrating realm %@", error);
        }
        [RLMRealmConfiguration setDefaultConfiguration:config];
    }

答案 1 :(得分:1)

RLMRealmConfiguration充当价值对象。应用于它的修改不会自动对defaultConfiguration生效。您只能检索它的副本。这意味着您必须使用setter来共享您的修改。