I'm implementing an app that persists data at some points (not related between them) using Realm. In example:
(Lets name each one of those points as a module/package)
Each module/package have some RealmObjects
to persist. How should I organize this? From the point of view of code cleanness, performance or whatever I should care
Option A: Use a unique (default) realm with a unique schema:
Use Realm.getInstance(context)
Accessing to the proper RealmObjects
in each module/package
Option B: Use multiple realms with the default schema
Specify a different name in the RealmConfiguration
for the realm used in each module (using the default schema).
Since the data belongs to different parts of the app, isolated and not interconnected, use different realm name for each module.
Option C: Use multiple realms and scope the model classes used with an schema per application package Specify a name and a schema for each isolated package. In example:
public static Realm getChat(Context context){
RealmConfiguration config = new RealmConfiguration.Builder(context)
.name("chat.realm")
.schemaVersion(1)
.setModules(new ChatRealmModule())
.build();
return Realm.getInstance(config);
}
// Create the module
@RealmModule(classes = { ChatRoom.class, ChatMessage.class, ChatUser.class})
public static class ChatRealmModule{
}
Option D: Other?
If your data is really completely disconnected, I would go with option C) It makes for a clean separation. Migrations are easier to handle, and there is also a very small performance gain as Realm has to loop through all model classes in a Realm from time to time.
But none of the options are "wrong".
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);
@RealmModule(classes={Abc.class, Pqrs.class, Xyz.class})
class MyCustomSchema{}
@RealmModule(classes={Abc1.class, Pqrs2.class, Xyz2.class})
class MyOtherSchema{}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With