领域:在应用程序中使用一个或多个领域(以及一个或多个模式) [英] Realm: Use one or multiple realms in an app (and one or multiple schemas)

查看:107
本文介绍了领域:在应用程序中使用一个或多个领域(以及一个或多个模式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Realm实现一个应用程序,该应用程序在某些点(它们之间不相关)中保留数据。例如:

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


  1. 保存用户收藏的项目。

  2. (应用聊天保存聊天对话和最近的常量

  3. 为应用程序的某些请求实现持久缓存

  4. 保存最近的搜索/表单以提供自动完成功能

  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)

每个模块/包都有一些 RealmObjects 要保留。我该如何组织这个?从代码清洁,性能或我应该关注的任何事情来看

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

选项A:使用具有唯一模式的唯一(默认)领域:

使用 Realm.getInstance(context)

在每个模块/包中访问正确的 RealmObjects

Accessing to the proper RealmObjects in each module/package

选项B:使用多个域默认架构

RealmConfiguration 中为每个模块中使用的域指定一个不同的名称(使用默认架构)。

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.

选项C:使用多个领域并使用每个应用程序包的模式使用的模型类
为每个隔离的包指定名称和模式。例如:

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

选项D:其他?

推荐答案

如果您的数据真的完全断开,我会选择C)
这样可以实现清晰的分离。迁移更容易处理,并且还有非常小的性能提升,因为Realm必须不时地遍历Realm中的所有模型类。

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.

但没有选项是错误的。

这篇关于领域:在应用程序中使用一个或多个领域(以及一个或多个模式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆