WWDC 2021: AsyncSequence vs. Combine

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

Part 1: WWDC 2021: AsyncSequence vs. Combine

01. Make IteratorProtocol async Using Combine

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Next episode: 02. Implement a Custom AsyncSequence

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.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Swift 5.5 is pretty amazing. The concurrency features are going to make life a lot easier on all of us, going forward.

// MARK: IteratorProtocol
extension EncodedModelIterator: IteratorProtocol {
  func next() -> Data? {
    <#code#>
  }
}
  }

  private var intIterator = (1...).makeIterator()
}
  func next() -> Data? {
    try? intIterator.next().map {
      try JSONEncoder().encode(<#T##value: Encodable##Encodable#>)
    }
  }
try JSONEncoder().encode(Model(int: $0))
extension EncodedModelIterator: Sequence, IteratorProtocol {
extension EncodedModelIterator: IteratorProtocol {
  }

  private let subject = PassthroughSubject<Data, Error>()
  private var intIterator = (1...).makeIterator()
  func next() -> Data? {
    do {
      guard let data = ( try intIterator.next().map {
        try JSONEncoder().encode(Model(int: $0))
      } ) else {
        return nil
      }

      return data
    } catch {
      return nil
    }
  }
      }

      subject.send(data)
      return data
    } catch {
      subject.send(completion: .failure(error))
      return nil
    }
  func resume() {
    Timer.publish(every: 2 / 3, on: .main, in: .default)
      .autoconnect()
      .sink { [unowned self] _ in _ = next() }
  }
  private var intIterator = (1...).makeIterator()
  private var cancel: (() -> Void)?
}
  func resume() {
    cancel = Timer.publish(every: 2 / 3, on: .main, in: .default)
      .autoconnect()
      .sink { [unowned self] _ in _ = next() }
      .cancel
  }
  func stop() {
    cancel?()
  }
final class EncodedModelIterator {
  var publisher: AnyPublisher<Data, Error> {
    subject.eraseToAnyPublisher()
  }

  func resume() {
struct ContentView {
  private let syncIterator = EncodedModelIterator()
}
struct ContentView {
  private let syncIterator = EncodedModelIterator()
  
  @State private var publishedModel: Model?
}
        Button {
          _ = syncIterator.next()
        } label: {
    VStack {
      IteratorView(
        title: "Combine",
        syncIterator: syncIterator,
        model: $publishedModel
      )
    }
      )
        .onReceive(
          syncIterator.publisher
            .decode(type: Model.self, decoder: JSONDecoder())
        ) {
          publishedModel = $0
        }
            .decode(type: Model.self, decoder: JSONDecoder())
            .map { $0 }
            .replaceError(with: nil)
        ) {