Por ejemplo:
Observable.fromCallable<Int> { backgroundTask() // returns an integer } .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe ({ number -> /* success */ }, { error -> /* fail */ })En general , hacer una tarea en segundo plano (otro hilo) y obtener el resultado en el hilo principal.
¿Cómo usará este fragmento de código las corrutinas de Kotlin ?
puede cambiar el hilo usando withContext() . Por ejemplo,
launch(Dispatchers.MAIN) { //main thread here val result = withContext(Dispatchers.IO) { //IO thread here backgroundTask() } //main thread here again //doing something with result }