Junior — Middle
Millised on piirangud kasutades niite Dispatchers.Main raames Coroutines?
sobes.tech AI
Vastus AI-lt
Dispatchers.Main in Kotlin Coroutines is designed to start coroutines on the main (UI) thread of Android.
Restrictions:
- Run only on the main thread — do not use Dispatchers.Main for background or long-running operations to avoid blocking the UI.
- Availability depends on the platform — on Android, Dispatchers.Main works with the main thread's Looper, in other environments, additional configuration may be required.
- Not suitable for CPU- or IO-intensive tasks — for these, it is better to use Dispatchers.IO or Dispatchers.Default.
Usage example:
CoroutineScope(Dispatchers.Main).launch {
// Update UI
textView.text = "Loading..."
val data = withContext(Dispatchers.IO) {
// Long operation
fetchData()
}
textView.text = data
}