Reputation: 2417
Today I changed my application realm schema, and so I implemented a Migration. Inside the migration, I just needed to add a field, so I did:
if (oldVersion == 0) {
RealmObjectSchema class = schema.get("Class");
class.addField("addedField", boolean.class, null)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
obj.set("addedField", false);
}
});
oldVersion++;
}
But this erased everything in my realm, I got no more data.
This is not a problem as I'm still in dev phase. I did the migration instead of uninstalling/reinstalling the app just to learn how to implement a RealmMigration.
Did I do something wrong? Is it normal that I got everything erased? Did I need to add some more code? I already read the documentation and had a look at the sample app. The code provided didn't got anything more.
Realm configuration:
Realm.init(this);
RealmConfiguration newConfig = new RealmConfiguration.Builder()
.name("myRealm.realm")
.schemaVersion(1)
.migration(new SchemaMigration())
.build();
And at least:
Realm realm = Realm.getInstance(newConfig);
I solved this by removing the name("myRealm.realm")
. At the first installation I didn't use a RealmConfiguration
so the name of the realm was: "default.realm". Thank you guys for your support and your answers!
Upvotes: 1
Views: 77
Reputation: 5361
Try do not pass null
as a field attributes and use type specific setter (setBoolean
) for transformation of DynamicRealmObject
:
if (oldVersion == 0) {
RealmObjectSchema class = schema.get("Class");
class.addField("addedField", boolean.class)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
obj.setBoolean("addedField", false);
}
});
oldVersion++;
}
Upvotes: 1