Bird
0
0

Why does withTaskGroup require an async closure parameter instead of returning a TaskGroup instance?

hard📝 Conceptual Q10 of 15
iOS Swift - Concurrency
Why does withTaskGroup require an async closure parameter instead of returning a TaskGroup instance?
ABecause TaskGroup instances are not allowed in Swift
BTo prevent any asynchronous code inside the closure
CTo force tasks to run synchronously inside the closure
DTo ensure all child tasks complete before the closure returns, providing structured concurrency
Step-by-Step Solution
Solution:
  1. Step 1: Understand structured concurrency

    withTaskGroup uses an async closure to scope child tasks and wait for completion.
  2. Step 2: Explain why no TaskGroup instance returned

    This design ensures all tasks finish before continuing, avoiding leaks or orphan tasks.
  3. Final Answer:

    To ensure all child tasks complete before the closure returns, providing structured concurrency -> Option D
  4. Quick Check:

    withTaskGroup enforces structured concurrency [OK]
Quick Trick: withTaskGroup closure waits for all tasks to finish [OK]
Common Mistakes:
  • Thinking TaskGroup instances can be stored and reused
  • Assuming tasks run synchronously inside closure
  • Believing async code is disallowed inside closure

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes