Răsfoiți Sursa

perf: 优化 runMain 方法的 Handler 使用单例

Reborn 1 an în urmă
părinte
comite
fb0bb1cbf8
1 a modificat fișierele cu 2 adăugiri și 1 ștergeri
  1. 2 1
      net/src/main/java/com/drake/net/utils/Suspend.kt

+ 2 - 1
net/src/main/java/com/drake/net/utils/Suspend.kt

@@ -58,10 +58,11 @@ suspend fun <T> withUnconfined(block: suspend CoroutineScope.() -> T) = withCont
 /**
  * 在主线程运行
  */
+private val mainThreadHandler by lazy { Handler(Looper.getMainLooper()) }
 fun runMain(block: () -> Unit) {
     if (Looper.myLooper() == Looper.getMainLooper()) {
         block()
     } else {
-        Handler(Looper.getMainLooper()).post { block() }
+        mainThreadHandler.post { block() }
     }
 }