Skip to content

Commit

Permalink
Merge pull request #2262 from Goooler/thread
Browse files Browse the repository at this point in the history
Add `isMainThread` extension to check thread
  • Loading branch information
pyricau authored Jan 4, 2022
2 parents 025af05 + 7b65e61 commit 429620d
Showing 1 changed file with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import android.os.Looper

internal val mainHandler by lazy { Handler(Looper.getMainLooper()) }

internal val isMainThread: Boolean get() = Looper.getMainLooper().thread === Thread.currentThread()

internal fun checkMainThread() {
check(Looper.getMainLooper().thread === Thread.currentThread()) {
check(isMainThread) {
"Should be called from the main thread, not ${Thread.currentThread()}"
}
}

internal fun checkNotMainThread() {
check(Looper.getMainLooper().thread !== Thread.currentThread()) {
check(!isMainThread) {
"Should not be called from the main thread"
}
}

0 comments on commit 429620d

Please sign in to comment.