|
| 1 | +<!-- Run this slideshow via the following command: --> |
| 2 | +<!-- reveal-md README.md -w --> |
| 3 | + |
| 4 | + |
| 5 | +<!-- .slide: class="header" --> |
| 6 | + |
| 7 | +# Combine |
| 8 | + |
| 9 | +## [Slides](https://make-school-courses.github.io/MOB-2.4-Advanced-Architectural-Patterns-in-iOS/Slides/Combine-Pt.1/README.html ':ignore') |
| 10 | + |
| 11 | +<!-- > --> |
| 12 | + |
| 13 | +▶️ Watch this before the lesson: |
| 14 | + |
| 15 | +[Combine - Video introduction](https://developer.apple.com/videos/play/wwdc2019/722/) |
| 16 | + |
| 17 | +<!-- > --> |
| 18 | + |
| 19 | +## Learning Objectives |
| 20 | + |
| 21 | +By the end of this lesson, you will be able to: |
| 22 | + |
| 23 | +Describe: |
| 24 | + - Reactive programming |
| 25 | + - Marble diagrams |
| 26 | + - Combine as a framework and it's main components |
| 27 | + - Basic operators |
| 28 | + - Lifecycle of a stream |
| 29 | + |
| 30 | +<!-- > --> |
| 31 | + |
| 32 | +## Reactive Programming |
| 33 | + |
| 34 | +Reactive Programming can be thought of as the practice of programming with **asynchronous** data streams, or __*event streams.*__ |
| 35 | + |
| 36 | +<!-- > --> |
| 37 | + |
| 38 | +### Event Streams |
| 39 | + |
| 40 | +An event stream is a **sequence of events** happening over time. |
| 41 | + |
| 42 | +An **asynchronous data stream** is a stream of data where values are *emitted*, one after another, with a delay between them, and without blocking program flow to wait for results. |
| 43 | + |
| 44 | +And because the stream is asynchronous, the data emitted can appear anywhere in time. |
| 45 | + |
| 46 | +<!-- > --> |
| 47 | + |
| 48 | +### Modeling Event Streams with Marble Diagrams |
| 49 | + |
| 50 | +The common way of modeling asynchronous streams is to place the emitted values on a time axis in what is called a **Marble Diagram** |
| 51 | + |
| 52 | +These are interactive diagrams that show how operators work with sequences over time. |
| 53 | + |
| 54 | +<!-- > --> |
| 55 | + |
| 56 | +Here, our diagram shows a simple description of a hypothetical event stream, with events represented by colored bubbles drawn at intermittent time intervals: |
| 57 | + |
| 58 | + </br> |
| 59 | + |
| 60 | +<!-- > --> |
| 61 | + |
| 62 | +In a Marble Diagram, the left-to-right arrow represents time, and the numbered circles represent elements of a sequence, which are just values plotted on the timeline. |
| 63 | + |
| 64 | +In the last diagram, pay particular attention to these symbols: |
| 65 | +- The **Error** symbol |
| 66 | +- The **Event Stream Complete** symbol |
| 67 | + |
| 68 | +<!-- > --> |
| 69 | + |
| 70 | +## Combine |
| 71 | + |
| 72 | +A framework that provides a declarative Swift API for processing values over time. |
| 73 | + |
| 74 | +These values can represent many kinds of asynchronous events. |
| 75 | + |
| 76 | +[Docs](https://developer.apple.com/documentation/combine) |
| 77 | + |
| 78 | +<!-- > --> |
| 79 | + |
| 80 | +## Core Concepts |
| 81 | + |
| 82 | +- [Publisher](https://developer.apple.com/documentation/combine/publisher) and [Subscriber](https://developer.apple.com/documentation/combine/subscriber) |
| 83 | + |
| 84 | +- Operators |
| 85 | + |
| 86 | +- [Subjects](https://developer.apple.com/documentation/combine/subject) |
| 87 | + |
| 88 | +<!-- > --> |
| 89 | + |
| 90 | +## Publisher |
| 91 | + |
| 92 | +- Described as a protocol |
| 93 | +- **Provides data when available and upon request** |
| 94 | +- A publisher that has not had any subscription requests will not provide any data |
| 95 | +- Has two associated types: one for **Output** and one for **Failure** |
| 96 | +- Value types |
| 97 | + |
| 98 | +<!-- > --> |
| 99 | + |
| 100 | +## Subscriber |
| 101 | + |
| 102 | +- Responsible for **requesting data and accepting the data** (and possible failures) provided by a publisher |
| 103 | +- Has two associated types: one for **Input** and one for **Failure** |
| 104 | +- Reference types (usually act and mutate state upon receipt of values) |
| 105 | + |
| 106 | +<!-- > --> |
| 107 | + |
| 108 | +<img src="https://heckj.github.io/swiftui-notes/images/diagrams/input_output.svg"> |
| 109 | + |
| 110 | +Publishers and subscribers are meant to be connected, and make up the core of Combine. |
| 111 | + |
| 112 | +<aside class="notes"> |
| 113 | +When you connect a subscriber to a publisher, both types must match: Output to Input, and Failure to Failure. |
| 114 | +</aside> |
| 115 | + |
| 116 | +<!-- > --> |
| 117 | + |
| 118 | +## Operator |
| 119 | + |
| 120 | +- An object that acts both like a subscriber and a publisher |
| 121 | +- Classes that adopt both the Subscriber protocol and Publisher protocol |
| 122 | +- They support subscribing to a publisher, and sending results to any subscribers |
| 123 | +- Can be chained to process, transform data |
| 124 | + |
| 125 | +<!-- > --> |
| 126 | + |
| 127 | +<img src="https://heckj.github.io/swiftui-notes/images/diagrams/pipeline.svg"> |
| 128 | + |
| 129 | +<!-- > --> |
| 130 | + |
| 131 | +```swift |
| 132 | +let _ = Just(8) |
| 133 | + .map { value -> String in |
| 134 | + // do something with the incoming value here |
| 135 | + return "\(value) as a string" |
| 136 | + } |
| 137 | + .sink { receivedValue in |
| 138 | + // sink is the subscriber and terminates the pipeline |
| 139 | + print("The end result was \(receivedValue)") |
| 140 | + } |
| 141 | +``` |
| 142 | + |
| 143 | +## In Class Activity |
| 144 | + |
| 145 | +Instructions [here]() |
| 146 | + |
| 147 | + |
| 148 | +<!-- > --> |
| 149 | + |
| 150 | +## After Class |
| 151 | + |
| 152 | + |
| 153 | +<!-- > --> |
| 154 | + |
| 155 | +## Additional Resources |
| 156 | + |
| 157 | +- [Using Combine](https://heckj.github.io/swiftui-notes/#coreconcepts-publisher-subscriber) |
| 158 | +- Book: Practical Combine by Donny Walls |
0 commit comments