Bird
0
0

You need to concurrently fetch profile info and recent activity for a user, then combine the results. Which Swift concurrency approach best fits this scenario?

hard📝 Application Q8 of 15
iOS Swift - Concurrency
You need to concurrently fetch profile info and recent activity for a user, then combine the results. Which Swift concurrency approach best fits this scenario?
AUse async let bindings for both fetches and await their results before combining
BCreate a task group and add tasks for each fetch, then combine results after group completion
CRun fetches sequentially using await one after another
DUse DispatchQueue.global().async to fetch concurrently and combine in main queue
Step-by-Step Solution
Solution:
  1. Step 1: Identify concurrency pattern

    For two concurrent async operations whose results are needed together, async let bindings provide a concise and structured way.
  2. Step 2: Compare options

    Use async let bindings for both fetches and await their results before combining uses async let to start both fetches concurrently and then awaits both results, which is efficient and clear.
  3. Step 3: Why not others?

    Create a task group and add tasks for each fetch, then combine results after group completion is valid but more complex for just two tasks. Run fetches sequentially using await one after another is sequential and inefficient. Use DispatchQueue.global().async to fetch concurrently and combine in main queue uses GCD, which is less safe and less structured than Swift concurrency.
  4. Final Answer:

    Use async let bindings for both fetches and await their results before combining -> Option A
  5. Quick Check:

    async let is ideal for a few concurrent async calls [OK]
Quick Trick: Use async let for few concurrent async calls [OK]
Common Mistakes:
  • Using task groups unnecessarily for few tasks
  • Running async calls sequentially
  • Using GCD instead of Swift concurrency

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes