0
0
iOS Swiftmobile~20 mins

Async sequences in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Sequence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this async sequence code?
Consider this Swift async sequence that emits numbers 1 to 3 with a delay. What will be printed?
iOS Swift
import Foundation

func printNumbers() async {
  for await number in AsyncStream<Int>({ continuation in
    Task {
      for i in 1...3 {
        try? await Task.sleep(nanoseconds: 100_000_000)
        continuation.yield(i)
      }
      continuation.finish()
    }
  }) {
    print(number)
  }
}

Task {
  await printNumbers()
}
A1\n2\n3
B3\n2\n1
CNo output, code deadlocks
D1 2 3 printed on the same line
Attempts:
2 left
💡 Hint
AsyncStream yields values in order, and the for-await loop prints each as it arrives.
lifecycle
intermediate
1:30remaining
What happens if you do not call continuation.finish() in AsyncStream?
In Swift's AsyncStream, what is the effect of omitting the call to continuation.finish() after yielding values?
AThe sequence throws a runtime error
BThe async sequence never ends and the for-await loop waits forever
CThe sequence ends immediately after the last yield
DThe sequence yields nil values after last yield
Attempts:
2 left
💡 Hint
Think about how the async sequence knows it is done.
📝 Syntax
advanced
2:00remaining
Which option correctly creates an AsyncStream of strings?
Choose the correct Swift code snippet that creates an AsyncStream emitting the strings "A", "B", "C".
A
let stream = AsyncStream&lt;String&gt; { continuation in
  ["A", "B", "C"].forEach { continuation.yield($0) }
  continuation.finish()
}
B
let stream = AsyncStream&lt;String&gt; { continuation in
  continuation.yield("A")
  continuation.yield("B")
  continuation.yield("C")
  continuation.finish()
}
C
let stream = AsyncStream&lt;String&gt; {
  continuation in
  continuation.yield("A")
  continuation.yield("B")
  continuation.yield("C")
}
D
let stream = AsyncStream&lt;String&gt; { continuation in
  for i in 0..&lt;3 {
    continuation.yield(String(i))
  }
  continuation.finish()
}
Attempts:
2 left
💡 Hint
Check which option yields all values and calls finish properly.
🔧 Debug
advanced
2:00remaining
What error occurs with this AsyncSequence code?
Given this Swift code snippet, what error will it produce when run?
iOS Swift
func test() async {
  let stream = AsyncStream<Int> { continuation in
    continuation.yield(1)
    continuation.yield(2)
    // Missing finish call
  }
  for await value in stream {
    print(value)
  }
}

Task {
  await test()
}
APrints 1 and 2 then exits normally
BCompilation error: missing continuation.finish()
CRuntime error: AsyncStream finished unexpectedly
DThe code runs and prints 1 and 2, then hangs waiting for more values
Attempts:
2 left
💡 Hint
What happens if the stream never signals completion?
🧠 Conceptual
expert
2:30remaining
How does AsyncSequence handle backpressure in Swift concurrency?
In Swift's concurrency model, how does AsyncSequence manage backpressure when producing values faster than the consumer can process?
AAsyncSequence drops values if the consumer is slow to avoid blocking
BAsyncSequence buffers all values until the consumer is ready, potentially using unlimited memory
CAsyncSequence suspends the producer until the consumer consumes values, preventing overflow
DAsyncSequence throws an error if the producer is faster than the consumer
Attempts:
2 left
💡 Hint
Think about how async/await suspends tasks in Swift.