简体   繁体   English

在 Kotlin 中使用实时数据和 RxJava 的领域

[英]Realm with live data and RxJava in Kotlin

I am trying to use Realm in a Kotlin based application.我正在尝试在基于 Kotlin 的应用程序中使用 Realm。

I set up a basic project, which tested adding elements into realm, and observing them with live data and a view model.我建立了一个基本项目,测试将元素添加到领域,并使用实时数据和视图模型观察它们。

Then I wanted to add in some other observables, and use combineLatest of RxJava to combine them.然后我想添加一些其他的可观察对象,并使用 RxJava 的 combineLatest 将它们组合起来。

I created Rx Observables for all observables.我为所有 observables 创建了 Rx Observables。 I observe the live data from Realm, and then call RxJava BehaviourSubject's onNext() method to set one of the Rx Observables to be the same value as the observed realm data.我观察来自 Realm 的实时数据,然后调用 RxJava BehaviourSubject 的 onNext() 方法将 Rx Observable 之一设置为与观察到的领域数据相同的值。 This works fine, but as soon as I added in a Throttle to the Rx Operators, I got the following error message:这工作正常,但是一旦我将 Throttle 添加到 Rx Operators,我收到以下错误消息:

Realm access from incorrect thread.从不正确的线程访问领域。 Realm objects can only be accessed on the thread they were created. Realm 对象只能在创建它们的线程上访问。 at io.realm.BaseRealm.checkIfValid(BaseRealm.java:442) at io.realm.com_example_myapplication_ItemRealmProxy.realmGet$id(com_example_myapplication_ItemRealmProxy.java:105) at io.realm.com_example_myapplication_ItemRealmProxy.toString(com_example_myapplication_ItemRealmProxy.java:661)在 io.realm.BaseRealm.checkIfValid(BaseRealm.java:442) 在 io.realm.com_example_myapplication_ItemRealmProxy.realmGet$id(com_example_myapplication_ItemRealmProxy.java:105) 在 io.realm.com_example_myapplication_ItemRealmProxy.toString(com_example_6.myapplication)

This is parts of my code:这是我的代码的一部分:

 private val itemViewModel: ItemViewModel by lazy {
        ViewModelProviders.of(this).get(ItemViewModel::class.java)
    }

    val itemsSubject = BehaviorSubject.createDefault(listOf<Item>())
    val intsSubject = BehaviorSubject.createDefault(4)



itemViewModel.getItemData().observe(this, Observer<RealmResults<Item>> { t ->
            val items = t.map {
                it
            }

            itemsSubject.onNext(items)
        })

  Observables.combineLatest(itemsSubject, intsSubject) { a, b ->
        a
    }.throttleLast(500, TimeUnit.MILLISECONDS).subscribe {
        Log.i("Items", "Items combined read: ${it}")
    }

I can't see how realm is coupled to the onNext call, nor the throttle.我看不到 Realm 是如何与 onNext 调用耦合的,也看不到油门。 Seems very weird.看起来非常奇怪。

Ideally I would like to stick to just Live data, but I need to do operations with other observables, and RxJava is good for this.理想情况下,我只想坚持使用实时数据,但我需要对其他可观察对象进行操作,而 RxJava 对此很有用。

I needed to observe the RxJava on the main thread, as follows:我需要在主线程上观察RxJava,如下:

 Observables.combineLatest(itemsSubject, intsSubject) { a, b ->
            a
        }.throttleLast(500, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe {
            Log.i("Items", "Items combined read: ${it}")
        }
    }

In a sense, the Realm error was misleading, because it was ultimately an incorrect use of RxJava.从某种意义上说,Realm 错误具有误导性,因为它最终是对 RxJava 的错误使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM