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

白昼怎懂夜的黑 提交于 2019-11-29 02:18:55

问题


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?


回答1:


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




回答2:


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{}


来源:https://stackoverflow.com/questions/33631605/realm-use-one-or-multiple-realms-in-an-app-and-one-or-multiple-schemas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!