https://kotlinlang.org logo
#coroutines
Title
# coroutines
u

uli

10/23/2017, 6:47 PM
Yes, looks like it 🙂 The beauty of coroutines is, that you have a sequential flow, even with suspension points.
m

Martin Tøften

10/23/2017, 7:44 PM
Hi again 🙂 Sorry to disturb you again. https://proandroiddev.com/android-coroutine-recipes-33467a4302e9 The article have two examples.
execute two tasks sequentially
and
execute two tasks parallel
. But I don't get the difference. Aren't both examples executed in parallel?
u

uli

10/23/2017, 7:47 PM
Nope, the difference is in the await
Task1 is awaited right where it is created. So the coroutine suspends and does not yet schedule Task2
Only after Task1 finishes, does the execution continue to fire Task2 to the background
So both examples run the tasks in the background. But the sequential example will only schedule one Task to the background at a time
m

Martin Tøften

10/23/2017, 7:51 PM
But my first example do the same thing.
fetchImagesAndSpaces()
does the same as the sequential example. Doesn't it? 🤔
u

uli

10/23/2017, 7:53 PM
No, you are doing as in the second example. You only await, after both Tasks are created: async, await async, await Compared to: async async await await
m

Martin Tøften

10/23/2017, 7:54 PM
Aha! I get it now. Thanks again 😄
2 Views