Probably has already been asked. Shouldn’t be always preferred to use withContext() instead of launch() ?
v
Vsevolod Tolstopyatov [JB]
11/05/2018, 6:28 PM
they are doing different things basically, so one cannot be preferred over another.
But if you end up asking yourself what should you use in this particular place of code, it’s probably
withContext
🙂
g
gildor
11/06/2018, 12:05 AM
Or just nothing and use suspend functions without context and dispatcher switching
d
droidrcc
11/06/2018, 10:06 AM
@gildor Do you mean use suspend function without launch a new coroutine? Shouldn’t be like calling a normal function in the same thread ?
g
gildor
11/06/2018, 10:08 AM
Yes, suspend function without creating new coroutines
It highly depends what you do in this suspend function. If you calling another suspend functions, than this is perfect solution, no need to start new coroutines
If you have blocking code (blocking io or cpu bound tasks) than wrap it to withContext with corresponding dispatcher
Use launch only top most level to start coroutines, or if you want to do something in parallel (or async if you do something in parallel and you need result)