CoroutineScope equivalent in swift concurrency

Kotlin has the concept of a CoroutineScope which allows you to launch Coroutines (equivalent to Swift Tasks) in them. The Coroutines inherit the CoroutineScope context implicitly. This is useful for when you have to launch multiple Coroutines in a single Android Activity (equivalent to UIKit UIViewController). In the Activity's onDestroy method, I can simply call coroutineScope.cancel() to cancel all Coroutines launched in it.

Watching the WWDC video on structured concurrency it seems the only thing mentioned equivalent to what I'd like to do in a UIViewController that's similar, is to store any Task created to a dictionary or list of tasks and call tasks.forEach { $0.cancel() }.

To conclude, is there no concept of a CoroutineScope in Swift concurrency that creates a top level instance, in a UIViewController say, where I can initiate child Tasks that implicitly inherit the context of the Scope that I can then call cancel on in the viewDidUnload method or another lifecycle method?

No, there's nothing that will implicitly pick up all local async work.

Terms of Service

Privacy Policy

Cookie Policy