I can use `delay()` in any coroutine, but not when...
# anko
h
I can use
delay()
in any coroutine, but not when using Anko's
doAsync { }
. What am I missing? How should I handle it?
l
I'd recommend to use
<http://Dispatchers.IO|Dispatchers.IO>
or
Dispatchers.Default
with
withContext
&
launch
(or
async
for parallelisation in same coroutine) instead.
h
OK. I have my Service implement
CoroutineScope
with a single threaded scope (
override val coroutineContext = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
), and voilà. (I believe a single thread is OK, since this is a fairly small routine that will only query the android downloadmanager for updates every 500 millis until a certain download is finished.) Thanks for pointing me in the right direction.
l
@hallvard Why don't you use
<http://Dispatchers.IO|Dispatchers.IO>
or
Dispatchers.Default
instead of a new executor?
h
Ah... because I didn't want to use
GlobalScope
, I guess, and once I added inheritance of
CoroutineScope
, I was asked by Android Studio to implement
val coroutineContext
, and before you knew it...
... so actually, I don't have to implement the CoroutineScope in my Service. If I understand this correctly, all I have to do, is
CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).launch { ... }
, and the effect will be the same as with Anko's
doAsync { ... }
, but I'm closer to the action myself, and can do
delay()
if I so wish. Nice.