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

巧了我就是萌 提交于 2019-12-13 16:09:16

问题


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

    ...
}

I did add a property to the user object, the docu says the new realm should automigrate, but i get a crash

*** 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

Version: 0.95

Note: When i updated to 0.96 i get

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

回答1:


Looks like adding

[RLMRealmConfiguration setDefaultConfiguration:config];

solved the issue, although not sure why

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



回答2:


RLMRealmConfiguration acts as value object. Modifications applied to it, don't automatically take effect to the defaultConfiguration. You can only retrieve a copy of it. That means you have to user the setter to share you modifications back.



来源:https://stackoverflow.com/questions/34242901/migrating-but-still-getting-crash-on-rlmexception-reason-migration-is-requ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!