Wieloplatformowa baza ViewModel
Przechowywanie stanu i zakresu używanego przez obie platformy
Wieloplatformowa baza ViewModel to bezpłatna lekcja Kotlin Multiplatform Academy na CoddyKit. To lekcja 1 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Kotlin Multiplatform Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. 🚀
Często zadawane pytania
Czy lekcja „Wieloplatformowa baza ViewModel” jest bezpłatna?
Tak — pełny tekst „Wieloplatformowa baza ViewModel” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Kotlin Multiplatform Academy, przejdź na CoddyKit PRO. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Wieloplatformowa baza ViewModel”?
Przechowywanie stanu i zakresu używanego przez obie platformy Ćwiczysz Kotlin Multiplatform Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Kotlin Multiplatform Academy?
Nie wymagamy żadnego doświadczenia. Kotlin Multiplatform Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 1 z 4.
Ile czasu zajmuje lekcja „Wieloplatformowa baza ViewModel”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Kotlin Multiplatform Academy?
Tak. Każda lekcja Kotlin Multiplatform Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Wieloplatformowa baza ViewModel
- Udostępnianie stanu interfejsu jako StateFlow
- Obsługa intencji i działań użytkownika
- Powiązanie ViewModel z każdym interfejsem