0Pricing
Kotlin Multiplatform Academy · レッスン

クロスプラットフォームViewModelの基底クラス

両プラットフォームで使える状態とscopeを保持します

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

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

Why Share the ViewModel?

A ViewModel holds screen state and logic. In KMP you write it once in shared code so both apps stay perfectly in sync. 🎉

Logic, Not Pixels

Your shared ViewModel decides what the screen shows. The native UI decides how to draw it. That split is the heart of KMP.

A Plain Class to Start

At its core a ViewModel is just a normal Kotlin class living in commonMain. No magic, no framework required to begin.

class CounterViewModel {
    var count = 0
}

It Needs a Scope

Async work needs a CoroutineScope. Every ViewModel keeps one so it can launch coroutines and cancel them later cleanly.

val scope = CoroutineScope(
    SupervisorJob() + Dispatchers.Main
)

SupervisorJob Keeps Siblings Alive

Using a SupervisorJob means one failing coroutine will not cancel the others in the same scope. Failures stay isolated.

A Reusable Base Class

Put the shared scope in an open base class so every screen's ViewModel inherits the same lifecycle plumbing for free.

open class BaseViewModel {
    protected val scope =
        CoroutineScope(SupervisorJob() + Dispatchers.Main)
}

Always Provide Cleanup

Add a clear() method that cancels the scope. When the screen goes away, the platform calls it so no work leaks.

open fun clear() {
    scope.cancel()
}

Extend the Base

Now a real screen just extends the base. It gets a ready scope and clean shutdown without repeating the boilerplate.

class HomeViewModel : BaseViewModel() {
    // screen logic here
}

androidx Has Its Own ViewModel

KMP now ships a multiplatform androidx.lifecycle.ViewModel. It gives you a managed viewModelScope on every target.

viewModelScope for Free

Extend that ViewModel and you get a built-in viewModelScope that is cancelled automatically when the ViewModel clears.

class HomeViewModel : ViewModel() {
    fun load() = viewModelScope.launch { }
}

One Class, Both Platforms

Whichever base you pick, the same shared ViewModel drives Android and iOS. Write the behavior once, trust it everywhere.

Quick Check

Let's lock in why a ViewModel carries a scope.

Recap

A shared ViewModel is a plain Kotlin class with a CoroutineScope and a clear() to cancel it. Build once, reuse on both apps. 🚀

よくある質問

「クロスプラットフォームViewModelの基底クラス」レッスンは無料ですか?

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

「クロスプラットフォームViewModelの基底クラス」で何を学びますか?

両プラットフォームで使える状態とscopeを保持します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「クロスプラットフォームViewModelの基底クラス」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. クロスプラットフォームViewModelの基底クラス
  2. UI StateをStateFlowとして公開する
  3. User IntentsとActionsを処理する
  4. ViewModelを各UIにバインドする
← Kotlin Multiplatform Academyに戻る