aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-12-30 16:19:41 -0500
committerAllan Wang <me@allanwang.ca>2018-12-30 16:19:41 -0500
commit3aa909f055c24d3700fa02f80c88e77a0c096f6e (patch)
tree8b700f0ccac3c526ec193002cf18f810d55257c3 /core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
parentf931e55d534e6162748f9fa888e7313cc2f0b619 (diff)
downloadkau-3aa909f055c24d3700fa02f80c88e77a0c096f6e.tar.gz
kau-3aa909f055c24d3700fa02f80c88e77a0c096f6e.tar.bz2
kau-3aa909f055c24d3700fa02f80c88e77a0c096f6e.zip
Build main dispatcher from handler, resolves #182
Diffstat (limited to 'core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt19
1 files changed, 13 insertions, 6 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
index 6867b49..09ad4ea 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
@@ -48,18 +48,25 @@ import ca.allanwang.kau.R
import ca.allanwang.kau.logging.KL
import com.afollestad.materialdialogs.MaterialDialog
import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.android.asCoroutineDispatcher
import kotlin.coroutines.CoroutineContext
/**
* Created by Allan Wang on 2017-06-03.
*/
-private object ContextHelper : CoroutineScope {
+object ContextHelper : CoroutineScope {
- val handler = Handler(Looper.getMainLooper())
+ val looper = Looper.getMainLooper()
- override val coroutineContext: CoroutineContext
- get() = Dispatchers.Main
+ val handler = Handler(looper)
+
+ /**
+ * Creating dispatcher from main handler to avoid IO
+ * See https://github.com/Kotlin/kotlinx.coroutines/issues/878
+ */
+ val dispatcher = handler.asCoroutineDispatcher("kau-main")
+
+ override val coroutineContext: CoroutineContext get() = dispatcher
}
/**
@@ -71,7 +78,7 @@ internal inline val Context.ctxCoroutine: CoroutineScope
get() = this as? CoroutineScope ?: ContextHelper
fun Context.runOnUiThread(f: Context.() -> Unit) {
- if (Looper.getMainLooper() === Looper.myLooper()) f() else ContextHelper.handler.post { f() }
+ if (ContextHelper.looper === Looper.myLooper()) f() else ContextHelper.handler.post { f() }
}
/**