Classical X
Classical X

Reputation: 149

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..

Upvotes: 2

Views: 3288

Answers (2)

Andy Dent
Andy Dent

Reputation: 17981

Yes you can, although you can would usually have multiple classes in on Realm

Configuring Other Reams shows how to specify different file paths, eg:

RealmConfiguration myConfig = new RealmConfiguration.Builder(context)
  .name("myrealm.realm")
  .schemaVersion(2)
  .modules(new MyCustomSchema())
  .build();

RealmConfiguration otherConfig = new RealmConfiguration.Builder(context)
  .name("otherrealm.realm")
  .schemaVersion(5)
  .modules(new MyOtherSchema())
  .build();

Realm myRealm = Realm.getInstance(myConfig);
Realm otherRealm = Realm.getInstance(otherConfig);

Upvotes: 6

Viraj Tank
Viraj Tank

Reputation: 1285

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 */

Upvotes: 4

Related Questions