Search in sources :

Example 1 with Migration

use of io.realm.examples.realmmigrationexample.model.Migration in project realm-java by realm.

the class MigrationExampleActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_realm_migration_example);
    rootLayout = ((LinearLayout) findViewById(R.id.container));
    rootLayout.removeAllViews();
    // 3 versions of the databases for testing. Normally you would only have one.
    copyBundledRealmFile(this.getResources().openRawResource(R.raw.default0), "default0.realm");
    copyBundledRealmFile(this.getResources().openRawResource(R.raw.default1), "default1.realm");
    copyBundledRealmFile(this.getResources().openRawResource(R.raw.default2), "default2.realm");
    // When you create a RealmConfiguration you can specify the version of the schema.
    // If the schema does not have that version a RealmMigrationNeededException will be thrown.
    RealmConfiguration config0 = new RealmConfiguration.Builder().name("default0.realm").schemaVersion(3).build();
    // You can then manually call Realm.migrateRealm().
    try {
        Realm.migrateRealm(config0, new Migration());
    } catch (FileNotFoundException ignored) {
    // If the Realm file doesn't exist, just ignore.
    }
    realm = Realm.getInstance(config0);
    showStatus("Default0");
    showStatus(realm);
    realm.close();
    // Or you can add the migration code to the configuration. This will run the migration code without throwing
    // a RealmMigrationNeededException.
    RealmConfiguration config1 = new RealmConfiguration.Builder().name("default1.realm").schemaVersion(3).migration(new Migration()).build();
    // Automatically run migration if needed
    realm = Realm.getInstance(config1);
    showStatus("Default1");
    showStatus(realm);
    realm.close();
    // or you can set .deleteRealmIfMigrationNeeded() if you don't want to bother with migrations.
    // WARNING: This will delete all data in the Realm though.
    RealmConfiguration config2 = new RealmConfiguration.Builder().name("default2.realm").schemaVersion(3).deleteRealmIfMigrationNeeded().build();
    realm = Realm.getInstance(config2);
    showStatus("default2");
    showStatus(realm);
    realm.close();
}
Also used : RealmConfiguration(io.realm.RealmConfiguration) Migration(io.realm.examples.realmmigrationexample.model.Migration) FileNotFoundException(java.io.FileNotFoundException) LinearLayout(android.widget.LinearLayout)

Aggregations

LinearLayout (android.widget.LinearLayout)1 RealmConfiguration (io.realm.RealmConfiguration)1 Migration (io.realm.examples.realmmigrationexample.model.Migration)1 FileNotFoundException (java.io.FileNotFoundException)1