Search code examples
javaandroidrealm

Multi realms database in 1 project


I'm wondering if I can add multiple realm databases to 1 project. for example 1# realm database for phone number, #2 for Books names , etc..


Solution

  • Yes, you can have multiple databases with Realm, but from the example you have given it looks like different RealmObject/Model class/tables in same database. But, if you need multiple databases this is how you can do it, by using multiple RealmConfiguration and using different Realm names, p.s. by default the name is default.realm,

    RealmConfiguration realmConfiguration1 = new RealmConfiguration.Builder(this)
                                                                   .name("realmName1")
                                                                   .build();   
    RealmConfiguration realmConfiguration2 = new RealmConfiguration.Builder(this)
                                                                   .name("realmName2")
                                                                   .build();
    
    Realm realm1 = Realm.getInstance(realmConfiguration1);
    /* do your operations in realm1 */
    Realm realm2 = Realm.getInstance(realmConfiguration2);
    /* do your operations in realm2 */