如何正确将Realm与应用程序的其余部分区分开? [英] How to properly separate Realm from the rest of the app?

查看:112
本文介绍了如何正确将Realm与应用程序的其余部分区分开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我试图将MVVM与存储库数据库一起使用.我喜欢保留所有外部依赖关系,并将它们分开并划分为它们自己的文件/模块,以便可以轻松地替换或替换它们.

In my app I am trying to use MVVM with repositories databases and all that. I like to keep all my external dependencies and such separate and compartmentalized into their own files/modules so that they can easily be replaced or swapped out.

使用Realm,我可以通过使用非托管对象来使这项工作非常好.例如,我可以有一个RealmHelper类,它仅打开一个领域实例,查询或执行一些事务,然后关闭该领域并返回一个对象.

With Realm I could make this work really well by using unmanaged objects. I can have a RealmHelper class for example which just opens a realm instance, queries or performs some transaction and then closes the realm and returns an object.

那么我该如何对托管对象完成类似的工作?问题是在这种情况下,您必须知道何时关闭领域.我认为这里最明显的解决方案是在完成数据库处理后让数据库知道,但这似乎是一个乏味且未优化的解决方案.还有另一种更好的方法吗?

So how can I accomplish something similar with managed objects? The problem is in this case that you have to know when to close the realm. The obvious solution here I think is to let the database know when you are done with it, but this seems like a tedious and unoptimized solution. Is there another better way?

推荐答案

所以我试图自己解决这个问题.我尚未对其进行很好的测试,但是我的想法基本上是从官方示例修改LiveRealmResults文件,以使调用方(例如RealmHelper)知道何时在非活动状态和活动状态之间更改状态.激活后,调用方将打开领域并传递结果.当它变为非活动状态时,调用者将关闭该领域.这是我的LiveRealmResults的样子:

So I have attempted to come up with a solution to this myself. I haven't tested it very well yet but my idea is basically to modify the LiveRealmResults file from the official example to let the caller (RealmHelper for example) know when it changes states between inactive and active. When it is active the caller will open the realm and pass in the results. When it changes to inactive the caller will close the realm. This is what my LiveRealmResults looks like:

@MainThread
class LiveRealmResults<T : RealmModel>(
    private val getResults: () -> RealmResults<T>,
    private val closeRealm: () -> Unit
) : LiveData<List<T>>() {

    private var results: RealmResults<T>? = null
    private val listener = OrderedRealmCollectionChangeListener<RealmResults<T>> { 
        results, _ ->

        this@LiveRealmResults.value = results
    }

    override fun onActive() {
        super.onActive()

        results = getResults()

        if (results?.isValid == true) {
            results?.addChangeListener(listener)
        }
        if (results?.isLoaded == true) {
            value = results
        }
    }

    override fun onInactive() {
        super.onInactive()

        if (results?.isValid == true) {
            results?.removeChangeListener(listener)
        }
        removeObserver()
    }
}

它将像这样使用:

class RealmHelper() {

    fun getObjects(): LiveData<List<Objects>> {
        var realm: Realm? = null

        return LiveRealmResults<Objects>(getResults = {

            realm = Realm.getDefaultInstance()
            realm!!.where<Objects>().findAll()
        }, removeObserver = {

            realm?.close()
        })
    }
}

此方法至少允许我将所有领域逻辑保留在RealmHelper中,仅公开LiveData而不公开RealmResults.每当LiveData处于非活动状态时,领域都会关闭.在我的示例中,我将返回RealmObject,但可以将RealmObject转换为普通对象,因此对于该示例,我不必担心该部分.

This method at least allows me to keep all realm logic in the RealmHelper, only exposing LiveData and not RealmResults. Whenever the LiveData is inactive the Realm is closed. In my example I'm returning RealmObject but I'm fine converting from RealmObject to normal object so I'm am not concerned with that part for this example.

这篇关于如何正确将Realm与应用程序的其余部分区分开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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