Modern Concurrency: Beyond the Basics

Oct 20 2022 Swift 5.5, iOS 15, Xcode 13.4

Part 1: AsyncStream & Continuations

5. Using a Buffered AsyncStream

Episode complete

Play next episode

Next
Save for later
About this episode
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 4. Using AsyncStream for Notifications Next episode: 6. Unit Testing

Use Xcode 13 for this exercise. Xcode 14 beta uses Swift 6, which doesn’t allow using countdown in the scheduledTRimer closure.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

Your challenge is to rewrite countdown(to:) using the buffered push-based version of AsyncStream.

let counter = AsyncStream<String> { continuation in
  var countdown = 3
  Timer.scheduledTimer(
    withTimeInterval: 1.0,
    repeats: true
  ) { timer in

  }
}
continuation.yield("\(countdown) ...")
countdown -= 1
) { timer in
  🟩guard countdown > 0 else {
    timer.invalidate()
    continuation.yield("🎉 " + message)
    continuation.finish()
    return
  }
  🟥continuation.yield("\(countdown) ...")
  countdown -= 1
}
continuation.yield(with: .success("🎉 " + message))