跨平台 ViewModel 基类
保存两个平台都能使用的状态和作用域
跨平台 ViewModel 基类 是 CoddyKit 上的免费 Kotlin Multiplatform Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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. 🚀
用 AI 导师学习 Kotlin — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 30
- 课程
- 120
常见问题解答
「跨平台 ViewModel 基类」课时是免费的吗?
是的 — 「跨平台 ViewModel 基类」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Kotlin Multiplatform Academy 课程的其余内容,请升级到 CoddyKit PRO。 Kotlin Multiplatform Academy 课程共包含 4 节课。
「跨平台 ViewModel 基类」这节课中我会学到什么?
保存两个平台都能使用的状态和作用域 你通过在浏览器中直接运行的动手代码来练习 Kotlin Multiplatform Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Kotlin Multiplatform Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Kotlin Multiplatform Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「跨平台 ViewModel 基类」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Kotlin Multiplatform Academy 课中编写并运行代码吗?
能。每节 Kotlin Multiplatform Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 跨平台 ViewModel 基类
- 将界面状态公开为 StateFlow
- 处理用户意图与操作
- 将 ViewModel 绑定到各个界面