พื้นฐาน ViewModel ข้ามแพลตฟอร์ม
เก็บสถานะและขอบเขตที่ทั้งสองแพลตฟอร์มใช้ได้
พื้นฐาน ViewModel ข้ามแพลตฟอร์ม เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ข้ามแพลตฟอร์ม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “พื้นฐาน ViewModel ข้ามแพลตฟอร์ม”
เก็บสถานะและขอบเขตที่ทั้งสองแพลตฟอร์มใช้ได้ คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “พื้นฐาน ViewModel ข้ามแพลตฟอร์ม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม
ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พื้นฐาน ViewModel ข้ามแพลตฟอร์ม
- เปิดเผยสถานะ UI เป็น StateFlow
- จัดการเจตนาและการกระทำของผู้ใช้
- ผูก ViewModel เข้ากับ UI แต่ละแบบ