Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

A Tour of Combine

311 views

Published on

A guided tour of Apple's new Combine framework for stream processing. We go over the main concepts and types and also explore how Combine implements back pressure.

Published in: Technology
  • Be the first to comment

A Tour of Combine

  1. 1. COMBINE A TOUR OF
  2. 2. VY-SHANE XIE I build software things at WiseTime https://vyshane.com I'm @vyshane on
  3. 3. The Combine framework provides a declarative Swift API for processing values over time. 
  4. 4. The Combine framework provides a declarative Swift API for processing values over time. 
  5. 5. STREAMS Publisher SubscriberOperator Subscriber Functional Reactive Programming FRP is a programming paradigm for asynchronous stream processing using the building blocks of functional programming (e.g. map, reduce, filter).
  6. 6. FRP RxSwift ReactiveSwift Is not new in the Swift ecosystem Combine
  7. 7. HOW IS COMBINE DIFFERENT?
  8. 8. COMBINE IS A foundational  framework Deeply integrated with SwiftUI Closed source Not available on Linux
  9. 9. https://forums.swift.org/t/will-swiftnio-adapt-to-the-new-combine-framework/25166/11 ONE MORE THING
  10. 10. REACTIVE STREAMS SPEC? Enables JVM streaming libraries to interoperate Provides a design for implementing back pressure
  11. 11. BACK PRESSURE?
  12. 12. userIDs getProfile save Slow IO
  13. 13. PRODUCER CONSUMERPROCESSOR CONSUMERuserIDs getProfile save Slow IO 😰 Incoming rate higher than we can process
  14. 14. PRODUCER CONSUMERuserIDs save Slow IO getProfile 💥We're out of memory
  15. 15. PRODUCER CONSUMERuserIDs save Slow IO getProfile On overflow drop elements Not Great! 😒
  16. 16. PRODUCER CONSUMERPROCESSOR CONSUMERgetProfile save Slow IO 🚦 😎 Signal demand upstream THIS IS BACK PRESSURE userIDs
  17. 17. COMBINE IS A low level  framework Deeply integrated with SwiftUI Closed source Not available on Linux The first Swift FRP library that has back pressure built in
  18. 18. KEY CONCEPTS COMBINE
  19. 19. SUBSCRIBEROperator 3 MAIN CONCEPTS Publisher Subscriber
  20. 20. PUBLISHER Knows how to deliver a stream of values over time Publisher
  21. 21. PUBLISHER The stream may complete successfully Publisher
  22. 22. PUBLISHER Or fail with an error Publisher
  23. 23. PUBLISHER An infinite stream that never ends is legal Publisher
  24. 24. PUBLISHER A stream that completes without emitting anything is OK Publisher
  25. 25. THE PUBLISHER PROTOCOL A publisher declares an Output type as well as a Failure type
  26. 26. SOME BUILT-IN WAYS TO PUBLISH VALUES
  27. 27. SUBSCRIBER ‣ Requests values from a publisher ‣ Acts on values as it receives them ‣ Declares an Input type and a Failure type
  28. 28. SUBSCRIBING TO A PUBLISHER A subscriber can subscribe to a publisher if: ‣ Its input type matches the output type of the publisher ‣ Its failure type matches that of the publisher Publisher Subscriber O F I F
  29. 29. SUBSCRIBING TO A PUBLISHER A subscriber can subscribe to a publisher if: ‣ Its input type matches the output type of the publisher ‣ Its failure type matches that of the publisher
  30. 30. TWO BUILT-IN SUBSCRIBERS
  31. 31. Assign Subscriber Assigns elements it receives to an object property at a keypath The assign(to:on:) operator creates an Assign subscriber and subscribes it to the upstream publisher
  32. 32. Sink Subscriber Accepts closures that receive values and completion events Useful when writing tests
  33. 33. OPERATORS Operate on streams and allow you to: ‣ Transform values ‣ Filter values ‣ Combine publishers together ‣ Manipulate timing ‣ Perform sequence and aggregation operations
  34. 34. Operators are instance methods on the Publisher protocol
  35. 35. Many operators subscribe to the upstream publisher and return a publisher of their own This allows you to compose them into pipelines EXAMPLE: CALLING A WEB SERVICE
  36. 36. EXAMPLE: CALLING A WEB SERVICE
  37. 37. EXAMPLE: CALLING A WEB SERVICE assign(to:on:) is a subscription operator, returns an AnyCancellable on which you can call .cancel()
  38. 38. RETURN TYPES OF CHAINED OPERATIONS
  39. 39. TYPE-ERASING OPERATOR Whew!
  40. 40. DEBUG OPERATORS Combine ships with some handy operators that you can insert into your pipelines to debug them: ‣ print(_:to:) ‣ breakpoint(receiveSubscription:receiveOutput: receiveCompletion:) ‣ breakpointOnError() ‣ handleEvents(receiveSubscription:receiveOutput: receiveCompletion:receiveCancel:receiveRequest:)
  41. 41. ... all yuor operators
  42. 42. SUBSCRIPTION LIFECYCLE COMBINE
  43. 43. PUBLISHER SUBSCRIPTION SUBSCRIBER Subscribes Creates Sends Requests values Emits value Returns demand Updates demand Emits value Demand fulfilled, pauses Returns demand Requests values Emits value Updates demand Returns demand No more values Emits completion Receives demand Stream ends Publishers are lazy (cold)
  44. 44. PUBLISHER SUBSCRIPTION SUBSCRIBER Subscribes Creates Sends Requests values Emits value Returns demand Receives demand Stream cancelled Deallocated
  45. 45. EXAMPLE - DEALLOCATED SUBSCRIPTION Times out, test fails
  46. 46. EXAMPLE - DEALLOCATED SUBSCRIPTION
  47. 47. CANCELLABLE ‣ Can be cancelled ‣ Can be stored/retained
  48. 48. SUPPORTED PLATFORMS Same as SwiftUI
  49. 49. SUPPORTED PLATFORMS Swift 5.1+ iOS 13.0+ macOS 10.15+ tvOS 13.0+ watchOS 6.0+ Mac Catalyst 13.0+
  50. 50. RESOURCES ‣ Using Combine web book (free): https://heckj.github.io/swiftui-notes/ ‣ Combine: Asynchronous Programming with Swift (ebook): https://store.raywenderlich.com/products/combine- asynchronous-programming-with-swift ‣ WWDC: Introducing Combine (video) https://developer.apple.com/videos/play/wwdc2019/722/ ‣ WWDC: Combine in Practice (video) https://developer.apple.com/videos/play/wwdc2019/721/
  51. 51. ANY QUESTIONS? THANKS FOR LISTENING

×