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

Migrating, but still getting crash on 'RLMException', reason: 'Migration is required

- (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:

看起来要添加

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

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