Upgrade to Pro — share decks privately, control downloads, hide ads and more …

An introduction to Combine

Eeb061c8b2816b771920da1b3e7904a3?s=47 Swift India
September 14, 2019

An introduction to Combine

Presented by Murali Kathir at Swift Bangalore meet up Chapter #17

Eeb061c8b2816b771920da1b3e7904a3?s=128

Swift India

September 14, 2019
Tweet

Transcript

  1. Introduction of Apple’s exciting new framework Combine

  2. Imperative vs Declarative Telling the machine how to do something

    and as a result what you want to happen will happen. Telling a machine what would like to happen and let the computer figure how to do it!
  3. None
  4. Async in Swift Grand Central Dispatch NSOperationQueue POSIX Threads Target

    / Action Callbacks / Completion Handlers
  5. Some factors about async in swift Hard to understand More

    manual effort Callback hell Hard to maintain
  6. Reactive Programming, which focuses on asynchronous data streams, which you

    can listen to and react accordingly. Functional Programming, which emphasizes calculations via mathematical-style functions, immutability and expressiveness, and minimizes the use of variables and state. FRP is the combination of functional and reactive paradigms. In other words, it is reacting to data streams using the functional paradigm. Eg: RxSwift, ReactiveSwift Functional Reactive Programming
  7. What is Combine? Combine is a new reactive framework created

    by Apple that streamlines how you can process asynchronous operations. Combine code is declarative and succinct, making it easy to write and read once you have a basic understanding of it. Combine also allows you to neatly organize related code in one place instead of having that code spread out across multiple files.
 

  8. Publisher The essence of Combine is: publishers send values to

    subscribers. To become a publisher, a type must adopt and conform to the Publisher protocol, which includes the following: associatedtype Output associatedtype Failure : Error
 
 Specifically, a publisher must define the type of values it will send, and its failure error type or Never if it will never send an error.
  9. How to create publishers Combine is integrated throughout the iOS

    SDK and Swift standard library. For example, this enables us to create a publisher from a NotificationCenter.Notification, or even an array of primitive values.
  10. Subscriber A subscriber attaches to a publisher to receive values

    from it. And the process is called Subscription. Subscribers must adopt and conform to the Subscriber protocol, which includes the following: associatedtype Input associatedtype Failure: Error
  11. How to create subscription There are two ways to create

    a subscription to a publisher: • By using one of the sink operators. • By using one of the assign(to:on:) operators.
  12. Example Usecase 1 Chaining Asynchronous Calls

  13. Example Usecase 2 Performing Multiple Asynchronous Calls

  14. Example Usecase 3 Merges Two Notifications and maps them

  15. How to stop subscriptions There are two ways to stop

    subscriptions: • Call cancel() on a subscription token. • Do nothing and let normal memory management rules to apply, i.e., the token or collection of AnyCancellable will call cancel() on the subscriptions upon deinitialization.
  16. None
  17. Major advantages Combine is integrated into many existing frameworks in

    iOS, macOS, watchOS, and tvOS. SwiftUI that relies heavily on Combine. The Foundation framework contains a lot of extensions to work with Combine. It allows to work with common types we are already familiar with: • A URLSessionTask Publisher that publishes the data response or request error • Operators for easy JSON decoding • A Publisher for a specific Notification.Name that publishes the notification
  18. Combine Combine Deployment Target iOS 13.0+ Platforms supported iOS, macOS,

    tvOS, watchOS Spec Reactive Streams (+ adjustments) Framework Consumption First-party (built-in) Maintained by Apple UI Bindings SwiftUI
  19. Thank you