https://kotlinlang.org logo
Title
a

aballano

04/03/2019, 9:34 AM
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:
// 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:
DEBUG onResume 0 main
DEBUG onResume 1 ForkJoinPool-1-worker-2
DEBUG onResume 2 DefaultDispatcher-worker-2
g

gildor

04/03/2019, 9:35 AM
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

aballano

04/03/2019, 9:37 AM
how would be the appropriate way to run this on Android then?
g

gildor

04/03/2019, 9:37 AM
just use
launch
instead
a

aballano

04/03/2019, 9:38 AM
but would that work on top of Arrow?
g

gildor

04/03/2019, 9:41 AM
I don’t know
In general you should start some background coroutine on background thread
a

aballano

04/03/2019, 9:42 AM
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

gildor

04/03/2019, 9:42 AM
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

pakoito

04/03/2019, 9:44 AM
No
launch
here @gildor, we’re doing our own coroutines runtime 😉
🍾 1
g

gildor

04/03/2019, 9:44 AM
I see
how you may launch async background task in Arrow?
p

pakoito

04/03/2019, 9:44 AM
runNonBlocking
👍 1
g

gildor

04/03/2019, 9:44 AM
😀
p

pakoito

04/03/2019, 9:44 AM
same file, same interface. next function 😛
🤦 1
g

gildor

04/03/2019, 9:44 AM
pretty stright-forward name 👍
g

gildor

04/03/2019, 9:45 AM
I see, yeah, make sense
s

simon.vergauwen

04/03/2019, 9:47 AM
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.