inline fun <T> ensureNotOnMainThread(block: () -> T): T {
check(Thread.currentThread() != Looper.getMainLooper().thread) { "This function cannot be called on main thread" }
return block()
}
允许用它包装任何方法,示例如下:
fun isInDataBase(id: String) = ensureNotOnMainThread { db.contains(id) }