ViewModel'de suspend Çağrıları
Verileri ana iş parçacığının dışında alın.
ViewModel'de suspend Çağrıları, CoddyKit'te ücretsiz bir Jetpack Compose Academy dersidir. Bu, 4 dersinin 3. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Jetpack Compose Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Jetpack Compose Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
Never Block the Main Thread
Network calls take time, and doing them on the UI thread freezes your app. You move that work off the main thread instead.
Make the Endpoint suspend
Mark your Retrofit function with the suspend keyword. Now it returns the parsed result directly and can pause without blocking. ⏸️
@GET("users")
suspend fun getUsers(): List<User>The ViewModel Owns the Logic
Fetching data belongs in the ViewModel, not the Composable. It survives rotation and keeps your UI code purely about display.
class UserViewModel : ViewModel() {
// network logic lives here
}Launch With viewModelScope
A suspend function needs a coroutine. viewModelScope gives you one that is cancelled automatically when the ViewModel clears.
viewModelScope.launch {
val users = api.getUsers()
}Store the Result in State
Once the call returns, save the data into a state property so Compose can observe it and redraw the screen.
var users by mutableStateOf<List<User>>(emptyList())Wrap It In a try-catch
Networks fail, so guard the call with try-catch. An unhandled exception in a coroutine can crash your whole app.
try {
users = api.getUsers()
} catch (e: Exception) {
// handle failure
}Fetch on Init
Trigger the first load in the ViewModel's init block, so data starts arriving the moment the screen is created.
init {
loadUsers()
}Keep It in a Function
Wrap the fetch in a named function like loadUsers. That makes it easy to call again for retry or pull-to-refresh.
fun loadUsers() = viewModelScope.launch {
users = api.getUsers()
}Dispatchers Handle Threading
Retrofit already runs the request on a background dispatcher, so you usually don't switch threads with Dispatchers.IO yourself.
Collect State in Compose
In your Composable, read the ViewModel's state. Because it's observable, Compose recomposes the instant fresh data lands.
val users = viewModel.usersCancellation Is Automatic
If the user leaves and the ViewModel clears, viewModelScope cancels the in-flight call, so you waste no work on a dead screen.
Quick Check
Which scope cancels its coroutines automatically when the ViewModel is cleared?
Recap: Calling From the ViewModel
You made endpoints suspend, launched them in viewModelScope, stored results in state, and caught errors. Next you'll render loading, success, and error states.
Sıkça Sorulan Sorular
“ViewModel'de suspend Çağrıları” dersi ücretsiz mi?
Evet — “ViewModel'de suspend Çağrıları” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Jetpack Compose Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Jetpack Compose Academy kursu toplamda 4 dersten oluşur.
“ViewModel'de suspend Çağrıları” dersinde ne öğreneceğim?
Verileri ana iş parçacığının dışında alın. Jetpack Compose Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Jetpack Compose Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Jetpack Compose Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 3. dersidir.
“ViewModel'de suspend Çağrıları” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Jetpack Compose Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Jetpack Compose Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Retrofit API Interface Tanımlama
- Moshi ile JSON Çözümleme
- ViewModel'de suspend Çağrıları
- Yükleme, Başarı ve Hata Kullanıcı Arayüzü