Thuta Learning
ရှာဖွေရန်
AdvancedMobile Developmentintermediate

Kotlin Coroutines & Flow

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Kotlin Coroutines & Flow ကို ကြောက်စရာမလိုအောင် နားလည်မယ်
  • ကိုယ်တိုင် Android Studio/Compose code ကို run ကြည့်တတ်မယ်
  • Real project ထဲမှာ ဒီ concept ကို ချက်ချင်း အသုံးချတတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

Coroutine က lightweight thread လိုမျိုးပါ — `suspend` function ကို `viewModelScope.launch { }` ထဲမှာ ခေါ်ရင် UI thread ကို block မလုပ်ဘဲ background မှာ run ပြီး, result ရလာရင် UI ကို update ပြန်ပေးပါတယ်။ Flow ကတော့ 'time ကြားထဲ value အများ ဆက်တိုက် ထုတ်ပေးတဲ့ stream' ကို ကိုယ်စားပြုပါတယ် (database query result auto-update, real-time search box) — Room DAO query ကို `Flow<List<TodoEntity>>` return type အနေနဲ့ ရေးထားရင်, database ထဲ data change ရင် UI ကို automatic update ပေးပါတယ်.

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

`@Query("SELECT * FROM TodoEntity") fun getAllAsFlow(): Flow<List<TodoEntity>>` လို့ Room DAO မှာ ရေးထားရင် — todo item အသစ် insert လုပ်တာနဲ့ (တခြား code ကနေတောင်), ViewModel ထဲက `.collect { todos -> ... }` က automatic trigger ဖြစ်ပြီး UI ကို live update ပေးပါလိမ့်မယ် — manual refresh button လိုအပ်တော့ပါ.

အတူတူ ကြည့်မယ်

kotlin
class TodoViewModel(private val dao: TodoDao) : ViewModel() {
    val todos: StateFlow<List<TodoEntity>> = dao.getAllAsFlow()
        .stateIn(
            scope = viewModelScope,
            started = SharingStarted.WhileSubscribed(5000),
            initialValue = emptyList()
        )

    fun addTodo(title: String) {
        viewModelScope.launch {
            dao.insert(TodoEntity(id = UUID.randomUUID().toString(), title = title, isDone = false))
        }
    }
}
You should see
`addTodo()` ခေါ်တာနဲ့ database update ဖြစ်ပြီး, UI ကို manual refresh မလိုဘဲ todos list က automatic update ဖြစ်နေတာ တွေ့ရမည်။

၅ မိနစ် စမ်းကြည့်

Room DAO (Advanced lesson 3) ကို `Flow<List<TodoEntity>>` return အောင် ပြောင်းရေးကြည့်ပါ — todo item insert ပြီးတိုင်း UI ကို manual refresh မလိုဘဲ auto-update ဖြစ်တာ confirm လုပ်ကြည့်ပါ။

သတိလေးတစ်ချက်

`GlobalScope.launch { }` ကို production code မှာ မသုံးပါနှင့် — Coroutine ရဲ့ lifecycle ကို control မရနိုင်တော့ပြီး, memory leak/crash risk ရှိပါတယ်; `viewModelScope`/`lifecycleScope` ကို အမြဲသုံးပါ။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • `suspend` function ကို Coroutine scope (`viewModelScope.launch`) ပြင်ပက တိုက်ရိုက် ခေါ်ကြိုးစားခြင်း — compile error ဖြစ်ပါလိမ့်မယ်
  • Flow ကို `collect` လုပ်ပြီးရင် cancel/cleanup မလုပ်ခြင်း — memory leak ဖြစ်နိုင်ပါတယ် (viewModelScope သုံးရင် ViewModel clear ဖြစ်တာနဲ့ auto-cancel ပါ)

အခု ကိုယ်တိုင် စမ်းကြည့်

Room DAO (Advanced lesson 3) ကို `Flow<List<TodoEntity>>` return အောင် ပြောင်းရေးကြည့်ပါ — todo item insert ပြီးတိုင်း UI ကို manual refresh မလိုဘဲ auto-update ဖြစ်တာ confirm လုပ်ကြည့်ပါ။

You'll know it worked when: `addTodo()` ခေါ်တာနဲ့ database update ဖြစ်ပြီး, UI ကို manual refresh မလိုဘဲ todos list က automatic update ဖြစ်နေတာ တွေ့ရမည်။

Kotlin Coroutines & Flow | Thuta Learning