0Pricing
Jetpack Compose Academy · บทเรียน

การเรียกแบบ suspend ใน ViewModel

ดึงข้อมูลนอกเธรดหลัก

การเรียกแบบ suspend ใน ViewModel เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Jetpack Compose Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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.users

Cancellation 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.

คำถามที่พบบ่อย

บทเรียน “การเรียกแบบ suspend ใน ViewModel” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเรียกแบบ suspend ใน ViewModel” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเรียกแบบ suspend ใน ViewModel”

ดึงข้อมูลนอกเธรดหลัก คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การเรียกแบบ suspend ใน ViewModel” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม

ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. กำหนดอินเทอร์เฟซ API ของ Retrofit
  2. แยกวิเคราะห์ JSON ด้วย Moshi
  3. การเรียกแบบ suspend ใน ViewModel
  4. ส่วนติดต่อผู้ใช้สถานะกำลังโหลด สำเร็จ และข้อผิดพลาด
← กลับไปที่ Jetpack Compose Academy