0Pricing
Jetpack Compose Academy · レッスン

Flowを返すリアクティブクエリ

Composeでデータベースを監視します。

「Flowを返すリアクティブクエリ」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJetpack Compose Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Jetpack Compose Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Static Reads Go Stale

A one-time query gives you data once, but the table keeps changing. You want the UI to refresh whenever rows are added or removed.

Return a Flow Instead

Change a DAO query to return a Flow. Room now emits a fresh list every single time the underlying table changes. 🔄

@Query("SELECT * FROM Note")
fun observeAll(): Flow<List<Note>>

No suspend on Flow Queries

A Flow query is not marked suspend. It returns the stream immediately, and emissions arrive as the data updates over time.

Room Watches the Tables

Behind the scenes Room tracks which tables a query touches. When any of them changes, it re-runs the query and emits a new value.

Collect the Flow Safely

In your ViewModel, collect the Flow inside a coroutine. Use viewModelScope so collection stops when the ViewModel clears.

viewModelScope.launch {
    dao.observeAll().collect { notes -> /* update state */ }
}

Turn Flow Into State

Convert the stream into observable UI state with stateIn, giving an initial value so the screen has something to show at once.

val notes = dao.observeAll().stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())

Observe It in Compose

In the Composable read the StateFlow with collectAsStateWithLifecycle. The list recomposes automatically on every change.

val notes by viewModel.notes.collectAsStateWithLifecycle()

Insert and Watch It Appear

Insert a row anywhere and the Flow emits again, so the new item shows up instantly without you refreshing manually.

Map Inside the Flow

You can transform results with map before they reach the UI, like sorting or filtering, all reactively in the stream.

dao.observeAll().map { it.sortedBy(Note::text) }

Flows Beat LiveData Here

Flow integrates cleanly with coroutines and Compose, making it the modern choice over LiveData for reactive Room queries.

One Source, Always Current

With reactive queries the database becomes your single source of truth, and the UI is just a reflection of it at every moment.

Quick Check

What happens when a Room DAO query returns a Flow?

Recap: Reactive Reads

You made queries return Flow, collected them into state, and watched Compose redraw on every change. Next you will organize this behind a repository.

よくある質問

「Flowを返すリアクティブクエリ」レッスンは無料ですか?

はい。「Flowを返すリアクティブクエリ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。

「Flowを返すリアクティブクエリ」で何を学びますか?

Composeでデータベースを監視します。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Jetpack Compose Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「Flowを返すリアクティブクエリ」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このJetpack Compose Academyレッスンでコードを書いて実行できますか?

はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. エンティティ、DAO、データベース
  2. Flowを返すリアクティブクエリ
  3. Repositoryパターン
  4. オフラインファーストのキャッシュ戦略
← Jetpack Compose Academyに戻る