Two realm database have same schema

In my project I have requirement of two separate database. Hence have create two separate realm database and I am inserting/updating/getting data by using Realm.getInstance(realmConfiguration). I have specified different models for both database but when I check the database in realm studio I can both databse schema have all the models which i have specified in the two different schema which 0 rows.

For example; realm1 I have two model model1 and model2 similarly for real2 db I have two model model3 and model4 and data is being insert in the specific model and also i can easily access this data from different model. But when i open these db files in realm studio I can see.
Realm1 db have 4 model model1, model2, model3 model4 and out of that model1 and model2 have data and model3 and model4 is empty.
Similarly, Realm 2 DB have 4 model model1, model2, model3 and model4, and in model3 and model4 have data while model1 and model2 is empty.

So, i am bit confuse why these models are creating in each other with 0 data? ideally realm1 should only have 2 molde model1 and model2 and similarly realm 2 should only have 2 model model3 and model4.

Can anyone guide me how I can fix this issue and make is separate for both the realm schema.

There’s likely an error in the code causing that, or perhaps the order in which tasks are being done.

As a test using Swift on a Mac, I created a fresh app with two Realm objects. Type0 and Type1.

In the UI, there are two buttons, button0 creates a Realm called realm0.realm and button1 creates a Realm realm1.realm.

In code realm0 is initted with a config that only specified the Type0 object, likewise, realm1 is initted with a config that only specifies Type1 object.

Upon pressing button 0, a realm file is created (realm0.realm) and only contains the type0 object. Pressing button 1 creates a realm1.realm file and only contains type1 object.

So, based on the description, I cannot duplicate the issue. Can you include some code that duplicates it?

1 Like

Hi Jay,

Below is my code. Please have a look:

realm1 database:

 val builder = RealmConfiguration.Builder()
            builder.name(realm1)
                .schemaVersion(1.2.0)
                .allowQueriesOnUiThread(true)
                .allowWritesOnUiThread(true)
                .addModule(realmModule1())
                .migration(MyMigration())
                builder.encryptionKey(realmKeyByte)
                config1 = builder.build()
                Realm.setDefaultConfiguration(config1)
                Realm.compactRealm(config1)

This is realmModule1 which added for realm1 database

@RealmModule(
    library = true,
    classes = [
        model1::class,
        model2::class,
    ]
)
class realmModule1

realm2 database:

val builder = RealmConfiguration.Builder()
            builder.name(realm2)
                .schemaVersion(1)
                .allowQueriesOnUiThread(true)
                .allowWritesOnUiThread(true)
                .addModule(realmModule2())
                builder.encryptionKey(realmKeyByte)
                config2 = builder.build()
                Realm.compactRealm(config2)

This is realmModule2 which added for realm1 database

@RealmModule(
    library = true,
    classes = [
        model3::class,
        model4::class,
    ]
)
class realmModule2

So, here you noticed that realm1 database is my default realm database and realm2 is my secondary database which I will access by the Realm.getInstance(realmConfig).

Please advice if there is any issue in above code

Perhaps off topic, but I think schemaVersion requires an integer (?)

.schemaVersion(1.2.0)

Also, I believe the default module includes all Realm objects defined in your application. If you want to use your own realms, you would define realm1 and realm2 and specify the objects they would contain.

Something like this

@RealmModule(library = false, classes=[Model1::class.java])
data class Module1(val someString: String)  { 
    //...
}

@RealmModule(library = false, classes=[Model2::class.java])
data class Module2(val someString: String)  {
    //...
}

val configOne = RealmConfiguration.Builder()
                      .name("first.realm")
                      .modules(Module1())
                      .build()

val configTwo = RealmConfiguration.Builder()
                      .name("second.realm")
                      .modules(Module2())
                      .build()

val realm1 = Realm.getInstance(configOne)
val realm2 = Realm.getInstance(configTwo)
1 Like