czuckie
11/08/2017, 10:39 AMclass 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()
}
}
dekans
11/08/2017, 11:15 AMasync(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.czuckie
11/08/2017, 11:22 AMdekans
11/08/2017, 1:38 PMawait()
, execution is dispatched to the UI thread