【SQLDelight 】Query を Flow 化するプラグイン

To consume a query as a Flow, depend on the Coroutines extensions artifact and use the extension method

Room + LiveData と同様に SQLDelight でも簡単にFlow化できます。


val players: Flow<List<HockeyPlayer>> =
  playerQueries.selectAll()
    .asFlow()
    .mapToList()

👉 Coroutines - SQLDelight

当然、クエリーの結果の変化を検知して emit します。


@JvmName("toFlow")
fun <T : Any> Query<T>.asFlow(): Flow<Query<T>> = flow {
  val channel = Channel<Unit>(CONFLATED)
  channel.trySend(Unit)

  val listener = object : Query.Listener {
    override fun queryResultsChanged() {
      channel.trySend(Unit)
    }
  }

  addListener(listener)
  try {
    for (item in channel) {
      emit(this@asFlow)
    }
  } finally {
    removeListener(listener)
  }
}

👉 FlowExtensions.kt#L35-L54

使えます。

👉 SQLDelight で View を使うべし 
👉 Turbine で Kotlin coroutine Flow をテストする | #android ファショ通 


Error: Please specify Xcode project location in xcodeproj property of gradle.properties

KMMで作ってみるかとドキュメント通りにやってみました。

👉 Getting started—Kotlin Multiplatform Mobile Docs 

iosApp の Run/Debug Configuration が自動で作られへん!

なんでや。

仕方なく手動で作って見ました。

scheme が読み込まれへん!

なんでや。

Fix ボタンで自動追記されましたが、明らかにおかしい。

なんでやねんな。

調べてみると、コマンドラインのツール選択ができてないとかなんとか。

👉 KMM plugin can't grab Xcode schemes : KT-41691 

再度、Run/Debug。

動かんやないか、なんでやの?

 

まとめ

プラグインのバグやないか。

It was fixed with ...

Situation is as follows.
If xcodeproj is not defined -- no iOS run configuration will be created.
If xcodeproj is defined then an iOS run configuration is created, but it is invalid and the reason is reported to user. This seems very logical: user deliberately targeted a Xcode project -- KMM tried it and reported back.

The fix is expected to be delivered in the next KMM plugin release (0.2.1).

👉 KMM plugin: Native run configuration is created for irrelevant (non-iOS) Native targets : KT-43792 


👉 KMM plugin releases—Kotlin Multiplatform Mobile Docs 

なんやこれ、待つしかないんか?

👉 Kotlin Multiplatform Mobile - Android Studio | JetBrains