Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

Looper.getMainLooper()-around extensions #533

Open
romtsn opened this issue May 10, 2018 · 1 comment
Open

Looper.getMainLooper()-around extensions #533

romtsn opened this issue May 10, 2018 · 1 comment

Comments

@romtsn
Copy link
Contributor

romtsn commented May 10, 2018

I'm not sure that this is a good idea, cause we'd have a new instance of Handler each time this function is called, but would it worth to have something like this:

inline fun postOnUiThread(
    crossinline action: () -> Unit
): Runnable {
    val runnable = Runnable { action() }
    Handler(Looper.getMainLooper()).post(runnable)
    return runnable
}

And also the following boolean might be useful in some cases:

inline val isUiThread: Boolean
    get() = Looper.getMainLooper().thread == Thread.currentThread()
@AllanWang
Copy link

Just as a reference to anko, they keep a reference of handler to avoid creating it each time. They will also avoid posting a runnable if you are already on the main thread

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants