0PricingLogin
Jetpack Compose Academy · Lesson

suspend Calls in the ViewModel

Fetch data off the main thread.

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>

All lessons in this course

  1. Define a Retrofit API Interface
  2. Parsing JSON with Moshi
  3. suspend Calls in the ViewModel
  4. Loading, Success & Error UI
← Back to Jetpack Compose Academy