Android Realm Migration Schema
Hello Guys, Long time no see… Today I’ll post some sort story about Realm. If you don’t know yet what is it, you can check those link :D
Ok, let’s talk with some small example. Sometimes, we have to make a Migration Schema for our local database instead “clear” them because of our application already on production right now.
So, what should we do ? exactly, Realm already has a Migration Schema to do that… I’ll show you how to implement that migration schema.
First thing first, make sure you already apply the realm plugin in your application like this… add this dependencies in your root build.gradle file
dependencies {
classpath 'io.realm:realm-gradle-plugin:2.3.0'
}
Then, apply that plugin in your project module
apply plugin: 'realm-android'
I’ll create a sample RealmObject here, just called it “UserData”
public class UserData extends RealmObject {
@PrimaryKey
private String email;
private String name;
public UserData() {
}
public void fill(final User user) {
setEmail(user.getEmail());
setName(user.getName());
}
public String getEmail() {
return email;
}
public void setEmail(final String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
}
Initialize your Realm Instance in Application class
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
final RealmConfiguration configuration = new RealmConfiguration.Builder().name("sample.realm").schemaVersion(1).build();
Realm.setDefaultConfiguration(configuration);
Realm.getInstance(configuration);
}
@Override
public void onTerminate() {
Realm.getDefaultInstance().close();
super.onTerminate();
}
}
To make it “safe”, I usually make the instance like this… means, just close the realm instance instead after application has been terminate :)
Last thing, don’t forget to make your own activity/fragment and register it in AndroidManifest. After you’ve run your application, you can do the transaction normally. Assume that your application already published on Play Store or somewhere else… and you want to add another field on the same RealmObject. I’ll show you how to do that without making your existing local database “gone”.
Let’s back to your RealmObject to add another field like this
public class UserData extends RealmObject {
@PrimaryKey
private String email;
private String name;
private int age;
public UserData() {
}
public void fill(final User user) {
setEmail(user.getEmail());
setName(user.getName());
setAge(user.getAge());
}
public String getEmail() {
return email;
}
public void setEmail(final String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(final int age) {
this.age = age;
}
}
So, we’ve already add a field called “age” right :) and after that create a RealmMigration class like this
public class RealmMigrations implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
final RealmSchema schema = realm.getSchema();
if (oldVersion == 1) {
final RealmObjectSchema userSchema = schema.get("UserData");
userSchema.addField("age", int.class);
}
}
}
You can see here that schema migration want to add another field on the same/existing RealmObject right :)
After that, change your Realm Configuration in your Application class like this
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
final RealmConfiguration configuration = new RealmConfiguration.Builder().name("sample.realm").schemaVersion(2).migration(new RealmMigrations()).build();
Realm.setDefaultConfiguration(configuration);
Realm.getInstance(configuration);
}
@Override
public void onTerminate() {
Realm.getDefaultInstance().close();
super.onTerminate();
}
}
You can see that I’m already upgrade the schema version and add a migration schema for Realm. Just build again your application and you can see what’s has been changed :D
Thanks anyway for reading my simple article, you can see the example about this article on My Github