I'm not sure if I'm doing something wrong or there...
# arrow-contributors
a
I'm not sure if I'm doing something wrong or there's something fishy when trying to run code on Android's main thread:
Copy code
// Android's fragment code
override fun onResume() {
    super.onResume()
    unsafe {
        runBlocking {
            fx {
                println("DEBUG onResume 0 $threadName")
                continueOn(NonBlocking)
                println("DEBUG onResume 1 $threadName")
                continueOn(Dispatchers.Default)
                println("DEBUG onResume 2 $threadName")
                continueOn(Dispatchers.Main)
                println("DEBUG onResume main $threadName")
            }
        }
    }
}
output is:
Copy code
DEBUG onResume 0 main
DEBUG onResume 1 ForkJoinPool-1-worker-2
DEBUG onResume 2 DefaultDispatcher-worker-2
g
Correct, because you use runBlocking
code inside of coroutine is dispatched on threads which you requested, but runBlockling blocks thread and wait child coroutine and suspend function
a
how would be the appropriate way to run this on Android then?
g
just use
launch
instead
a
but would that work on top of Arrow?
g
I don’t know
In general you should start some background coroutine on background thread
a
the thing is that switching to other threads works as seen in the output, the problem is when trying to go back to the main thread
and the idea is to do it on top of arrow (reason why I ask here 🙂)
g
your problem is runBlocking
not thread switching
runBlocking blocks thread exactly what you shouldn’t do on main thread
but if you use runBlocking, it means that you already have dependency on kotlinx.coroutines, isn’t it?
p
No
launch
here @gildor, we’re doing our own coroutines runtime 😉
🍾 1
g
I see
how you may launch async background task in Arrow?
p
runNonBlocking
👍 1
g
😀
pretty stright-forward name 👍
p
same file, same interface. next function 😛
🤦 1
g
I see, yeah, make sense
s
Soon we'll add interop with kotlinx again. So you'll be able to run fx code with launch/async.
But you might lose the optimisation we have in our runners. Hard to 100% confirm that already.