Group Group Group Group Group Group Group Group Group

Conclusion | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/8458165-kotlin-coroutines-fundamentals/lessons/11

This was a really good introduction to coroutines. I’m still not entirely sure why we need nested coroutines. This maybe due to the fact that I skipped my operating system lectures in college while we covered thread switching and signal handling/processing, and less about the course itself. Could you possibly explain why we need nested coroutines again?

Hey @altincamp !

Nested coroutines are useful when you want to run multiple different things in parallel, and it can go two ways.

First is having multiple async/await patterns, where you have complex data fetching. For example if you, within a launch block, want to fetch two data pieces.

And one of the data pieces has to fetch from three different endpoints to combine the data. In that case, you’d want to go with nested coroutines, to maximize the parallelization between your calls.

Secondly, you might want to run multiple fire & forget calls, where, once again, one of the calls has to launch a few nested coroutines.

An example might be analytics. You launch a coroutine on Main to start your work. Your first nested coroutine (launch) is Analytics reporting, and the second can be another launch for crash reporting.

Within either of the reporting coroutines, you can have multiple different nested coroutines to reportto different services for analytics/crashlytics.

It’s a bit far fetched, but yeah, it’s useful in specific use cases!