Realm for Android的迁移:如何正确添加RealmListField

时间:2017-06-08 10:33:54

标签: android realm realm-migration

在之前的RealmDB中,我有Program表,如:

public class Program extends RealmObject {
    @Expose
    @SerializedName("episodes")
    @ParcelPropertyConverter(RealmListParcelConverter.class)
    public RealmList<Episode> episodes;

    @Expose
    @SerializedName("performers")
    @Ignore
    public List<Performer> performers;
}    

和表演者表:

public class Performer {

    @Expose
    @SerializedName("id")
    public Long id;

    @Expose
    @SerializedName("name")
    public String name;

}

在新版本中,我想将performers属性添加到数据库中。所以我实现了像这样的迁移:
在MyApplication中:

private void initRealm() {
        Realm.init(this);
        RealmConfiguration config = new RealmConfiguration
                .Builder()
                .schemaVersion(1)
                .migration(new OnsMigration())
                .build();
        Realm.setDefaultConfiguration(config);
    }

和OnsMigration类:

public class OnsMigration implements RealmMigration {
    @Override
    public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
        if (newVersion == 1) {
            RealmSchema schema = realm.getSchema();
            RealmObjectSchema performerSchema = schema.create("Performer")
                    .addField("id", Long.class)
                    .addField("name", String.class)
            RealmObjectSchema programSchema = schema.get("Program");
            programSchema.addRealmListField("performers", performerSchema);
        }
    }
}

但是program.getPerformers()仍然是Null 如何正确自定义迁移?
谢谢

0 个答案:

没有答案