Android's UI

Android's UI dispatcher has a rather interesting implementation. First's there's a HandlerContext class that takes an android.os.Handler and a name. And the dispatch function simply forwards the Runnable to the post() function of the handler:

public class HandlerContext(
private val handler: Handler,
private val name: String? = null
) : CoroutineDispatcher(), Delay {

override fun dispatch(context: CoroutineContext, block: Runnable) {
handler.post(block)
}
}

Then, an instance of it called UI is created. It passes the main looper and the name UI to the constructor:

val UI = HandlerContext(Handler(Looper.getMainLooper()), "UI")
In practice, the only dispatcher that should override isDispatchNeeded() is Unconfined. All the other dispatchers should always enforce a thread or pool of threads.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.56.12