【问题标题】:RealmMigrationNeededException whileRealmMigrationNeededException 而
【发布时间】:2017-11-28 15:55:10
【问题描述】:

我有一个使用以前版本的领域 (v0.85.1) 的实时应用程序,我正在开发一个使用领域 4.2.0 版本的新版本,并且还进行了架构更改。架构更改只是添加到一个表中的单个列。对于迁移,我有这门课。

public class ImRealmMigration implements RealmMigration{
    @Override
    public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {

        RealmSchema schema = realm.getSchema();

        if(oldVersion == 0){
            schema.get("Layout")
                    .addField("orientation", String.class, FieldAttribute.REQUIRED);
            oldVersion++;
        }
    }
}

在应用程序类中我有这个

 Realm.init(this);
        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
                .name("default.realm")
                .schemaVersion(1)
                .migration(new ImRealmMigration())
                .build();
        Realm.setDefaultConfiguration(realmConfiguration); // Make this Realm the default

        Realm realm = Realm.getInstance(realmConfiguration);

当我运行该应用程序时,它会一直运行到最后一行抛出 RealmMigrationNeededException 并继续列出架构中的所有主键,说明它们已被设为可选。有谁知道为什么会发生这种情况以及我能做些什么来解决它?这是堆栈跟踪:

                FATAL EXCEPTION: main  
                    java.lang.RuntimeException: 
                Unable to start activity io.realm.exceptions.RealmMigrationNeededException: 

        Migration is required due to the following errors:

- Property 'Book.privateId' has been made optional.
                                                                                                       - Property 'BookType.bookTypeId' has been made optional.
                                                                                                       - Property 'Cart.id' has been made optional.
                                                                                                       - Property 'ClipArt.url' has been made optional.
                                                                                                       - Property 'Contact.userId' has been made optional.
                                                                                                       - Property 'CreditAccount.id' has been made optional.
                                                                                                       - Property 'Layout.layoutId' has been made optional.
                                                                                                       - Property 'LayoutClipArtItem.layoutItemId' has been made optional.
                                                                                                       - Property 'LayoutItem.itemId' has been made optional.
                                                                                                       - Property 'LayoutPhotoItem.layoutItemId' has been made optional.
                                                                                                       - Property 'LayoutShapeItem.layoutItemId' has been made optional.
                                                                                                       - Property 'LayoutTextItem.layoutItemId' has been made optional.
                                                                                                       - Property 'OrderSession.id' has been made optional.
                                                                                                       - Property 'PagePhoto.pagePhotoId' has been made optional.
                                                                                                       - Property 'Photo.privateId' has been made optional.
                                                                                                       - Property 'PhotoOrigin.identifier' has been made optional.
                                                                                                       - Property 'PrintPack.privateId' has been made optional.
                                                                                                       - Property 'PrintType.printTypeId' has been made optional.
                                                                                                       - Property 'Product.productId' has been made optional.
                                                                                                       - Property 'ProductFamilyMarketing.productFamilyType' has been made optional.
                                                                                                       - Property 'Region.rid' has been made optional.
                                                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3151)
                                                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3261)
                                                                                                           at android.app.ActivityThread.access$1000(ActivityThread.java:219)
                                                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1735)
                                                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                           at android.os.Looper.loop(Looper.java:145)
                                                                                                           at android.app.ActivityThread.main(ActivityThread.java:6939)
                                                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                                                                                                        Caused by: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
                                                                                                       - Property 'Book.privateId' has been made optional.
                                                                                                       - Property 'BookType.bookTypeId' has been made optional.
                                                                                                       - Property 'Cart.id' has been made optional.
                                                                                                       - Property 'ClipArt.url' has been made optional.
                                                                                                       - Property 'Contact.userId' has been made optional.
                                                                                                       - Property 'CreditAccount.id' has been made optional.
                                                                                                       - Property 'Layout.layoutId' has been made optional.
                                                                                                       - Property 'LayoutClipArtItem.layoutItemId' has been made optional.
                                                                                                       - Property 'LayoutItem.itemId' has been made optional.
                                                                                                       - Property 'LayoutPhotoItem.layoutItemId' has been made optional.
                                                                                                       - Property 'LayoutShapeItem.layoutItemId' has been made optional.
                                                                                                       - Property 'LayoutTextItem.layoutItemId' has been made optional.
                                                                                                       - Property 'OrderSession.id' has been made optional.
                                                                                                       - Property 'PagePhoto.pagePhotoId' has been made optional.
                                                                                                       - Property 'Photo.privateId' has been made optional.
                                                                                                       - Property 'PhotoOrigin.identifier' has been made optional.
                                                                                                       - Property 'PrintPack.privateId' has been made optional.
                                                                                                       - Property 'PrintType.printTypeId' has been made optional.
                                                                                                       - Property 'Product.productId' has been made optional.
                                                                                                       - Property 'ProductFamilyMarketing.productFamilyType' has been made optional.
                                                                                                       - Property 'Region.rid' has been made optional.
                                                                                                           at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(Native Method)
                                                                                                           at io.realm.internal.OsSharedRealm.<init>(OsSharedRealm.java:172)
                                                                                                           at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:219)
                                                                                                           at io.realm.BaseRealm.<init>(BaseRealm.java:124)
                                                                                                           at io.realm.BaseRealm.<init>(BaseRealm.java:93)
                                                                                                           at io.realm.Realm.<init>(Realm.java:153)
                                                                                                           at io.realm.Realm.createInstance(Realm.java:424)
                                                                                                           at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:342)
                                                                                                        at io

【问题讨论】:

    标签: android realm realm-migration


    【解决方案1】:

    0.89.0

    重大变化

    @PrimaryKey 字段值现在可以为 String、Byte、Short、 Integer 和 Long 类型。应该迁移较旧的领域,使用 RealmObjectSchema.setNullable(),或者通过添加@Required注解 (#2515)。

    查看版本0.89.0的更改日志

    您可以通过在 @PrimaryKey 字段中添加 @Required 轻松绕过此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多