Hey guys :wave: can someone clarify something for ...
# android
c
Hey guys 👋 can someone clarify something for me, I've been trying to give coroutines a go, but I feel how I've got it working might not be 100% right
Copy code
class MainActivity : AppCompatActivity() {

    suspend fun fetchTextRemote() = async {
        URL("<https://httpbin.org/ip>").openConnection().getInputStream().bufferedReader().readText()
    }

    fun fetchText() = async(UI) {
        maintext.text = fetchTextRemote().await()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        fetchText()
    }
}
d
the
async(UI)
is not necessary You can replace it by
launch(UI, CoroutineStart.UNDISPATCHED)
As you don't need the result and it doesn't require an immediate dispatch.
c
when you say it doesn't require an immediate dispatch, as in, it could be executed at the leisure of the event loop?
d
launch(UI), is basically posting runnable to a handler. No dispatch means that code is executed immediately
but right after the
await()
, execution is dispatched to the UI thread