ViewModel VS Coroutine 범위

작성자

카테고리:

← 피드로
DEV Community · Manoj sai Challagulla · 2026-06-23 개발(SW)

Manoj sai Challagulla

1. Coroutine

A coroutine is the actual background task.

Example:

launch {
    api.getProfile()
}

Enter fullscreen mode Exit fullscreen mode

Here, the work is:

api.getProfile()

So coroutine means:

A lightweight task that can run without blocking the main thread.

2. CoroutineScope

A CoroutineScope is like a container/manager for coroutines.

It decides:

Where coroutine runs
How long coroutine lives
When coroutine should be cancelled

Enter fullscreen mode Exit fullscreen mode

Example:

val scope = CoroutineScope(Dispatchers.IO)

scope.launch {
    // coroutine work
}

Enter fullscreen mode Exit fullscreen mode

Here:

scope = manager
launch block = coroutine

Enter fullscreen mode Exit fullscreen mode

3. viewModelScope

viewModelScope is a special CoroutineScope given by Android to every ViewModel.

Example:

class ProfileViewModel : ViewModel() {

    fun loadProfile() {
        viewModelScope.launch {
            repository.getProfile()
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Meaning:

Run this coroutine inside the ViewModel’s scope.

4. Relationship

Enter fullscreen mode Exit fullscreen mode

Very important:

CoroutineScope is the general concept.

viewModelScope is one specific CoroutineScope.

Coroutine is the actual task running inside the scope.

Enter fullscreen mode Exit fullscreen mode

Like this:

ViewModel
   owns
viewModelScope
   manages
coroutines

Enter fullscreen mode Exit fullscreen mode

Visual:

ProfileViewModel

└── viewModelScope

├── coroutine 1 → API call
├── coroutine 2 → DB operation
└── coroutine 3 → file operation

5. Why use viewModelScope?

Because it automatically cancels coroutines when ViewModel is cleared.

viewModelScope.launch {
    delay(10000)
    println("Done")
}

Enter fullscreen mode Exit fullscreen mode

If user leaves the screen before 10 seconds:

Screen closed
↓
ViewModel cleared
↓
viewModelScope cancelled
↓
Coroutine cancelled

Enter fullscreen mode Exit fullscreen mode

So “Done” will not print.

6. Without viewModelScope

val scope = CoroutineScope(Dispatchers.IO)

scope.launch {
    delay(10000)
    println("Done")
}

Enter fullscreen mode Exit fullscreen mode

Here you created the scope manually.

So you must cancel it manually:

scope.cancel()

Enter fullscreen mode Exit fullscreen mode

Otherwise it may continue even after the screen is gone.

7. Final Note

Write this in your notes:

Coroutine = actual background task.

CoroutineScope = manager/container that controls coroutines.

viewModelScope = Android-provided CoroutineScope attached to ViewModel.

When ViewModel is cleared, viewModelScope is automatically cancelled.

So all coroutines launched inside viewModelScope are also cancelled.

Enter fullscreen mode Exit fullscreen mode

Best simple line:

viewModelScope.launch { } means:

Run this coroutine as long as the ViewModel is alive.

Enter fullscreen mode Exit fullscreen mode

원문에서 계속 ↗

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다