带有Kotlin-协程的房间观察数据库变化 [英] Room with Kotlin-coroutines observe db changes

查看:188
本文介绍了带有Kotlin-协程的房间观察数据库变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我最近开始进行协程的试验,我从Rxjava2切换到协程,我还没有掌握它,但是仍然遇到了一种情况,我需要观察数据库的变化并更新相应的UI

So, I recently started experimentation with coroutines, I switched from Rxjava2 to coroutines, I haven't got a grasp of it yet but still, I ran into a condition where I needed to observe my database change and update the UI corresponding to that.

RxJava过去曾向我提供Flowables,Completeable等,使我能够观察到Db的变化.

RxJava used to provide me with Flowables, Completeable etc. using that I would be able to observe changes in Db.

    abstract fun insert(data: SomeData): Long

    @Query("SELECT * FROM somedata_table")
    abstract fun getData(): Flowable<List<SomeData>>

所以现在我在这里订阅了getData,并且总是用来观察更改

So here now I used to subscribe to getData and always used to observe changes

现在输入协程,我正在使用具有延迟结果的暂停函数返回我的回复

Now Enter coroutines, I am using a suspended function with a deferred result to return my responses

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    abstract fun insert(data: SomeData): Long

    @Query("SELECT * FROM somedata_table")
    abstract fun getData(): List<SomeData>

suspend fun getAllSomeData():Deferred<List<SomeData>>{
        return GlobalScope.async (context= coroutineContext){
            database.myDao().getData()
        }
    }

现在我无法收听更新,协程中的频道可能是正确的答案?但我不确定如何在Room中使用它.

Now I have no way to listen for updates, Channels in coroutines might be the right answer? but I am not sure how to use it with Room.

推荐答案

使用 Room 2.2.0流程 kotlin协程.它有争议,但我不喜欢 LiveData ,因为它可以在UI线程上为您提供结果.如果必须执行任何数据解析,则必须将所有内容推回另一个IO线程.与直接使用频道相比,它还更干净,因为每次您想收听事件时都必须执行额外的 openSubscription().consumeEach {..} 呼叫.

Use Room 2.2.0 Flows and kotlin coroutines. It's contentious but I dislike LiveData as it gives you results on the UI thread. If you have to do any data parsing you'll have to push everything back to another IO thread. It's also cleaner than using channels directly as you have to do extra openSubscription().consumeEach { .. } calls every time you want to listen to events.

流方法需要以下版本:

//此版本在其非实验版本中使用协程和流程

// this version uses coroutines and flows in their non-experimental version

org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2
androidx.room:room-runtime:2.2.0
androidx.room:room-compiler:2.2.0

道:

@Dao
interface MyDao {
     @Query("SELECT * FROM somedata_table")
     fun getData(): Flow<List<SomeData>>
}

课堂观察:

launch {
   dao.getData().collect { data ->
    //handle data here
   }
}

如果您的调用类本身不是 CoroutineScope ,则必须在上下文上下文中调用launch.可以是 GlobalScope 或您创建的其他一些类.

if your calling class is not itself a CoroutineScope you'd have to call launch with the context of something that is. That can be GlobalScope or some other class you create.

GlobalScope.launch {
   dao.getData().collect { data ->
    //handle data here
   }
}

收集 lambda会像在Rx onNext 调用中一样,接收到表中的每个udpate.

the collect lambda will receive every udpate to the table much like an Rx onNext call.

这篇关于带有Kotlin-协程的房间观察数据库变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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