协程
Flow
要将查询作为 Flow 消费,请依赖 Coroutines 扩展构件并使用其提供的扩展方法:
kotlin
kotlin {
sourceSets.commonMain.dependencies {
implementation("app.cash.sqldelight:coroutines-extensions:2.1.0")
}
}
groovy
kotlin {
sourceSets.commonMain.dependencies {
implementation "app.cash.sqldelight:coroutines-extensions:2.1.0"
}
}
kotlin
val players: Flow<List<HockeyPlayer>> =
playerQueries.selectAll()
.asFlow()
.mapToList(Dispatchers.IO)
这个 Flow
会发出查询结果,并且每当数据库针对该查询发生变化时,都会发出新的结果。