Coroutine yönlendiricileri ile optimize işlemler yapma
🔮 Neden Kullanılır
✨ Optimize edilmiş thread yapısıdır
👮♂️ Main (UI), IO, Default thread yapıları ile arka plan işlemlerini yönetirsiniz
🦄 newSingleThreadContext ile tek örneği olan coroutine yapısı oluşturulur
⭐ Dispatcher Sınıfları
🧱 Main
🔣 IO
🎳 Default
🌌 Unconfined
UI Thread işlemleri
Disk ve network işlemleri
CPU gerektiren işlemler
Main thread işlemleri
Fonksiyon çağırma
Database
Liste sıralama
Tanımlanmamıştır
View işlemleri
Dosya okuma & yazma
JSON parsing
LiveData işlemleri
Ağ işlemleri
DiffUtils
👨💻 Kullanım Örneği
import kotlinx.coroutines.*funmain() =runBlocking<Unit> {launch { // context of the parent, main runBlocking coroutineprintln("main runBlocking :\ I'm working in thread ${Thread.currentThread().name}") }launch(Dispatchers.Unconfined) { // not confined -- will work with main threadprintln("Unconfined :\ I'm working in thread ${Thread.currentThread().name}") }launch(Dispatchers.Default) { // will get dispatched to DefaultDispatcher println("Default :\ I'm working in thread ${Thread.currentThread().name}") }launch(newSingleThreadContext("MyOwnThread")) { // will get its own new threadprintln("newSingleThreadContext:\ I'm working in thread ${Thread.currentThread().name}") } }/*Sırası değişken olabilir (?)Unconfined : I'm working in thread mainDefault : I'm working in thread DefaultDispatcher-worker-1newSingleThreadContext: I'm working in thread MyOwnThreadmain runBlocking : I'm working in thread main (Her zaman en son çalışır)*/
import kotlinx.coroutines.*funlog(msg: String) =println("[${Thread.currentThread().name}] $msg")funmain() {newSingleThreadContext("Ctx1").use { ctx1 ->newSingleThreadContext("Ctx2").use { ctx2 ->runBlocking(ctx1) {log("Started in ctx1")withContext(ctx2) {log("Working in ctx2") }log("Back to ctx1") } } } }/*[Ctx1 @coroutine#1] Started in ctx1[Ctx2 @coroutine#1] Working in ctx2[Ctx1 @coroutine#1] Back to ctx1*/
👨👨👦👦 İç İçe Coroutine
👨👦 İç coroutine işlemleri bitmeden ana coroutine tamamlanmaz
🧐 GlobalScope bağımsız olarak çalışır
🙄 İçinde bulunduğu thread bitse bile devam eder
🌇 GlobalScope.launch {} coroutine'i işi bitince sonlanır (daemon)
import kotlinx.coroutines.*funmain() =runBlocking<Unit> {// launch a coroutine to process some kind of incoming requestval request =launch {// GlobalScope üzerinde coroutine tanımlama GlobalScope.launch {println("job1: I run in GlobalScope and execute independently!")delay(1000)println("job1: I am not affected by cancellation of the request") }// Run blocking context'ini miras alan coroutine tanımlamalaunch {delay(100)println("job2: I am a child of the request coroutine")delay(1000)println("job2: I will not execute this line if my parent request is cancelled") } }delay(500) request.cancel() // Coroutine objesini durdurmadelay(1000) // delay a second to see what happensprintln("main: Who has survived request cancellation?")}/*job1: I run in GlobalScope and execute independently!job2: I am a child of the request coroutinejob1: I am not affected by cancellation of the requestmain: Who has survived request cancellation?*/
➕ Context ile Birleştirme
launch(Dispatchers.Default +CoroutineName("test")) {println("I'm working in thread ${Thread.currentThread().name}")}