需要迁移领域:偶尔出错

时间:2017-03-30 18:44:32

标签: android migration realm

我有已发布作品的应用

目前正在使用的Realm版本是1.1.0

添加Firebase崩溃报告后,我开始遇到奇怪的错误:

  

io.realm.exceptions.RealmMigrationNeededException:The   此Realm的架构中缺少MedicineSchedulerItem类。

     

无法启动接收器   BootReceiver:   io.realm.exceptions.RealmMigrationNeededException:The   此Realm的架构中缺少MedicineSchedulerItem类。

领域迁移程序

@Override
    public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {

        RealmSchema schema = realm.getSchema();

        if (oldVersion == 0) {

            schema
                    .get("DoctorVisit")
                    .addField("docType", String.class);

            oldVersion++;

        }

        if (oldVersion == 1) {

            schema
                    .create("Message")
                    .addField("id", String.class, FieldAttribute.PRIMARY_KEY)

                    .addField("text_ru", String.class)
                    .addField("text_en", String.class)
                    .addField("text_ua", String.class)

                    .addField("title_ru", String.class)
                    .addField("title_en", String.class)
                    .addField("title_ua", String.class)

                    .addField("date", long.class)
                    .addField("download", int.class)
                    .addField("isVisible", Boolean.class)
                    .addField("isRead", Boolean.class);

            oldVersion++;

        }

        if (oldVersion == 2) {

            schema.get("DoctorVisit")
                    .addField("useNotifications", boolean.class);

            schema.create("MedicineSchedulerItem")
                    .addField("id", String.class, FieldAttribute.PRIMARY_KEY)
                    .addField("parentId", String.class)
                    .addField("masterId", String.class)
                    .addField("dosage", float.class)
                    .addField("time", long.class)
                    .addField("alarmId", int.class)
                    .addField("isVisible", boolean.class);

            schema
                    .create("MedicineCourse")
                    .addField("id", String.class, FieldAttribute.PRIMARY_KEY)
                    .addField("userId", String.class)
                    .addField("profileId", String.class)
                    .addField("comment", String.class)
                    .addField("isVisible", boolean.class)
                    .addField("updateTime", long.class)
                    .addField("title", String.class)
                    .addField("manufacturingForm", int.class)
                    .addField("dosage", float.class)
                    .addField("measurementUnit", int.class)
                    .addField("dateStart", long.class)
                    .addField("duration", int.class)
                    .addField("takingIntervalDays", int.class)
                    .addField("takingTime", long.class)
                    .addField("takingTimeType", long.class)
                    .addField("useNotifications", boolean.class)
                    .addRealmObjectField("medicine", schema.get("Medicine"))
                    .addRealmListField("scheduler", schema.get("MedicineSchedulerItem"))
                    .addRealmListField("dayScheduler", schema.get("MedicineSchedulerItem"));


            oldVersion++;

        }

    }

大多数设备都能正常运行,迁移过程应该是应有的。 但在极少数情况下,我会收到上面提到的错误。

我使用' Realm.getDefaultInstance()'获取领域实例,启动初始化:

RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
                .name(Realm.DEFAULT_REALM_NAME)
                .schemaVersion(3)
                .migration(new ProjectRealmMigration())
                .build();
        Realm.setDefaultConfiguration(realmConfiguration);

1 个答案:

答案 0 :(得分:0)

这不是一个多进程问题。出现此错误是因为您更新了Realm对象接口(Realm Schema)。如果您不想使用迁移来解决问题,可以通过转到AVD Manager,选择您使用的虚拟设备来修复它,单击右侧的箭头并单击“擦除数据”。
注意:这将删除此设备上的所有其他数据,因此您可能会考虑改为创建新的虚拟设备。