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

An introduction to Combine

Swift India
September 14, 2019

An introduction to Combine

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

Swift India

September 14, 2019
Tweet

More Decks by Swift India

Other Decks in Technology

Transcript

  1. 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!
  2. Some factors about async in swift Hard to understand More

    manual effort Callback hell Hard to maintain
  3. 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
  4. 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.
 

  5. 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.
  6. 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.
  7. 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
  8. 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.
  9. 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.
  10. 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
  11. 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