WWDC 2021: AsyncSequence vs. Combine

Jul 6 2021 · Swift 5.5, iOS 15, Xcode 13 Beta 2

Part 1: WWDC 2021: AsyncSequence vs. Combine

02. Implement a Custom AsyncSequence

Episode complete

About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 01. Make IteratorProtocol async Using Combine

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 02. Implement a Custom AsyncSequence

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

Before continuing on, you’ll want to have a decent handle on Swift 5.5’s async/await features. Brian Moakley’s got you covered, with an introductory video, linked in the Author Notes.

final class AsyncEncodedModelSequence {

}
// MARK: - AsyncSequence, AsyncIteratorProtocol
extension AsyncEncodedModelSequence: AsyncIteratorProtocol {

}
extension AsyncEncodedModelSequence: AsyncIteratorProtocol {
  typealias Element = Data


}
  typealias Element = Data

  func next() async -> Data? {
    <#code#>
  }
}
  func next() async throws -> Data? {
    try await withCheckedThrowingContinuation { continuation in

    }
  }
final class AsyncEncodedModelSequence {
  private var continuation: CheckedContinuation<Data?, Error>?
}
    try await withCheckedThrowingContinuation { continuation in
      self.continuation = continuation
    }
extension AsyncEncodedModelSequence: AsyncSequence, AsyncIteratorProtocol {
  }
}

public extension AsyncSequence where AsyncIterator == Self {
  func makeAsyncIterator() -> Self { self }
}
  }

  let syncIterator = EncodedModelIterator()

  private var continuation: CheckedContinuation<Data?, Error>?
  }

  let syncIterator = EncodedModelIterator()

  private var cancellable = AnyCancellable { }
  private var continuation: CheckedContinuation<Data?, Error>?
final class AsyncEncodedModelSequence {
  init() {
    cancellable = syncIterator.publisher
      .sink(
        receiveCompletion: <#T##((Subscribers.Completion<Error>) -> Void)##((Subscribers.Completion<Error>) -> Void)##(Subscribers.Completion<Error>) -> Void#>,
        receiveValue: <#T##((Data) -> Void)##((Data) -> Void)##(Data) -> Void#>
      )
  }

  let syncIterator = EncodedModelIterator()
        receiveCompletion: { [unowned self] completion in
          switch completion {
          case .finished:
            continuation?.resume(returning: nil)
            continuation?.resume(returning: nil)
          case .failure(let error):
            continuation?.resume(throwing: error)
          }
        },
        receiveValue: { [unowned self] value in
          continuation?.resume(returning: value)
        }
      )
struct ContentView {
  private let syncIterator = EncodedModelIterator()
  private let asyncSequence = AsyncEncodedModelSequence()
  
  @State private var publishedModel: Model?
  @State private var asyncModel: Model?
}
          publishedModel = $0
        }
      
      Divider()
        .padding()
      
      IteratorView(
        title: "Async",
        syncIterator: asyncSequence.syncIterator,
        model: $asyncModel
      )
    }
      )
        .task {
          for try await model in asyncSequence {

          }
        }
    }
        .task {
          do {
            for try await model in asyncSequence {

            }
          } catch { }
        }
            for try await model in (
              asyncSequence
                .map { try JSONDecoder().decode(Model.self, from: $0) }
            ) {
              asyncModel = model
            }