领域将交易变为可观察的

时间:2017-01-17 15:21:33

标签: android realm rx-java kotlin

有时我想在使用Retrofit进行服务调用后执行写操作。由于我的默认服务器调用是在io线程上执行的,我只想使用相同的线程进行Realm写操作。

我正在使用它:

serviceInteractor.uploadMeasure(measure)
        .filter { uploaded: Boolean -> uploaded } // filter only the ones successfully uploaded
        .flatMap { realmInteractor.markMeasureSent(measure.timeStamp) }
        .subscribe(
                { success -> /* do nothing */ },
                { inDebug { it.printStackTrace() } }
        )

My Realm Observable如下:

fun markMeasureSent(measureId: Long?): Observable<Unit> {
        if (measureId == null) Observable.just(Unit)

        return Observable.fromCallable {
            Realm.getDefaultInstance().use {
                it.executeTransaction { tRealm ->
                    assertNotMainThread()
                    val measure = tRealm.where(Item::class.java)
                            .equalTo(item, measureId)
                            .findFirst()

                    measure.sent = 1
                }
            }
        }
    }

这是将Realm Transaction包装到observable中的正确方法吗?

0 个答案:

没有答案