Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm: Use one or multiple realms in an app (and one or multiple schemas)

I'm implementing an app that persists data at some points (not related between them) using Realm. In example:

  1. Save the items favorited by the user.
  2. (The app have a chat) Save the chat conversations and recent constants
  3. Implement a persistent cache for some requests of the app
  4. Save recent searches/form in order to provide autocompletion

(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?

like image 209
Addev Avatar asked Nov 10 '15 13:11

Addev


2 Answers

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

like image 195
Christian Melchior Avatar answered Oct 13 '22 19:10

Christian Melchior


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{}
like image 24
Vijendra Kumar Sewaliya Avatar answered Oct 13 '22 20:10

Vijendra Kumar Sewaliya