Bird
0
0

You want to create an async sequence that emits numbers 1 to 3 with a 1-second delay between each. Which approach correctly implements this?

hard📝 Application Q8 of 15
iOS Swift - Concurrency
You want to create an async sequence that emits numbers 1 to 3 with a 1-second delay between each. Which approach correctly implements this?
AUse a DispatchQueue to yield values synchronously
BUse a synchronous sequence and call sleep(1) between yields
CUse AsyncStream but yield all values immediately without delay
DUse AsyncStream and call continuation.yield inside Task.sleep for delay
Step-by-Step Solution
Solution:
  1. Step 1: Understand async delay in AsyncStream

    To delay between yields asynchronously, use Task.sleep inside the AsyncStream closure.
  2. Step 2: Eliminate synchronous or immediate yield options

    Options A, B, and C do not provide asynchronous delays correctly.
  3. Final Answer:

    Use AsyncStream and call continuation.yield inside Task.sleep for delay -> Option D
  4. Quick Check:

    Async delay requires Task.sleep inside AsyncStream [OK]
Quick Trick: Use Task.sleep for async delays in AsyncStream yields [OK]
Common Mistakes:
  • Using synchronous sleep causing blocking
  • Yielding all values immediately
  • Using DispatchQueue for synchronous yields

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes