January 2018
Intermediate to advanced
434 pages
14h 1m
English
The doAsync returns a Java Future. In simple words, a Future is a proxy or a wrapper around an object that is not yet there. When the asynchronous operation is done, you can extract it. If you want to avoid working with Future, doAsync has a different construct that accepts an ExecutorService:
val executor = Executors.newScheduledThreadPool(5)doAsync(executorService = executor){ val result = URL("https://httpbin.org/get").readText() uiThread { toast(result) }}
As we discussed, the uiThread block isn't executed if the activity is closing. The reason is that it doesn't hold a context instance, only a weak reference. So even if the block isn't finished, the context will not leak.
Read now
Unlock full access