Building an AsyncSequence with AsyncStream.makeStream

Поделиться
HTML-код
  • Опубликовано: 5 окт 2024
  • In this video you'll learn how to use Swift 5.9's AsyncStream.makeStream method to build custom async sequences. You'll learn about its usage as well as its caveats.
    This video is a companion for this blog post: www.donnywals....
    Other blog posts worth reading:
    Understanding Swift Concurrency's AsyncStream: www.donnywals....
    Understanding Swift Concurrency's AsyncSequence: www.donnywals....
    Iterating over web socket messages with async / await in Swift: www.donnywals....
    Comparing lifecycle management for async sequences and publishers: www.donnywals....

Комментарии • 5

  • @priceringo1756
    @priceringo1756 6 месяцев назад +1

    Thank for such great, simple but not trivial, examples!

  • @indiekiduk
    @indiekiduk 6 месяцев назад

    I think makeStream is an anti-pattern because I ran into issues when using like cancellation not working. Once I redesigned my async code and made AsyncStream own the object that yields values all my problems were solved!

    • @DonnyWalsdev
      @DonnyWalsdev  6 месяцев назад

      Any chance you have a gist that would demonstrate the issues you've had? The tests I ran involved moving from the older AsyncStream init to makeStream and I didn't see these issues.

    • @indiekiduk
      @indiekiduk 6 месяцев назад

      @@DonnyWalsdev ok I'm trying to work on that. I noticed you had onTermination = { [weak self] that must have been to work around the retain cycle issue.

    • @indiekiduk
      @indiekiduk 6 месяцев назад

      I've been investigating this all day and I think makeStream is fine. By using a weak var in the parent object and using onTerminate to capture a strong reference to the stream's object I can make cancellation deinit the stream's object properly. My test harness has a task group with 2 tasks that call the same stream property on its containing object (to make sure the first stream is cancelled). I can only have one stream working at a time despite the object's stream property being a computed var that returns a new stream, so I wanted it to also cancel a previous stream.