You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Lessons/Combine-Pt.1/README.md
+112Lines changed: 112 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,6 +73,10 @@ These values can represent many kinds of asynchronous events.
73
73
- Has two associated types: one for **Output** and one for **Failure**
74
74
- Value types
75
75
76
+
<asideclass="notes">
77
+
The idea of publishers is so generic that we can represent any data: making calculations, exxecuting and handling network calls, reacting to user input, displaying data, etc.
78
+
</aside>
79
+
76
80
<!-- > -->
77
81
78
82
## Subscriber
@@ -81,6 +85,13 @@ These values can represent many kinds of asynchronous events.
81
85
- Has two associated types: one for **Input** and one for **Failure**
82
86
- Reference types (usually act and mutate state upon receipt of values)
83
87
88
+
<!-- v -->
89
+
90
+
## Built-in Subscribers
91
+
92
+
-[Assign](https://heckj.github.io/swiftui-notes/#reference-assign) - let's us **bind** the result to some property in our model or on a UI control via a key path.
93
+
-[Sink](https://heckj.github.io/swiftui-notes/#reference-sink) - let's us pass closures and completion handlers (you can do anything here 😎)
@@ -93,13 +104,45 @@ When you connect a subscriber to a publisher, both types must match: Output to I
93
104
94
105
<!-- > -->
95
106
107
+
## Subscription
108
+
109
+
We need a [Subscription](https://developer.apple.com/documentation/combine/subscription) How does it work?
110
+
111
+
A subscriber activates the publisher. Note that publishers won't emit values if there are no subscribers.
112
+
113
+
When you successfully create a subscription it can almost work on it's own.
114
+
115
+
A subscription will get activated when an event occurs (gestures, timers, etc.) and then a publisher reacts to the event.
116
+
117
+
</aside>
118
+
119
+
<!-- > -->
120
+
121
+
## Memory management
122
+
123
+
There is no need to manually memory manage subscriptions. The protocol `Cancellable` is used to indicate that the return object of a subscription is `Cancellable`.
124
+
125
+
When that object is released from memory, the entire subcsription is cancelled and memory resources are released. 🗑
126
+
127
+
<!-- > -->
128
+
129
+
## Coding examples
130
+
131
+
Playground Demo for Publishers & Subscribers
132
+
133
+
<!-- > -->
134
+
96
135
## Operator
97
136
98
137
- An object that acts both like a subscriber and a publisher
99
138
- Classes that adopt both the Subscriber protocol and Publisher protocol
100
139
- They support subscribing to a publisher, and sending results to any subscribers
101
140
- Can be chained to process, transform data
102
141
142
+
<asideclass="notes">
143
+
Operators focus on handling data they receive from a previous operator and give an input to the next one in the pipeline. This means there is no shared state.
@@ -148,6 +191,32 @@ A publisher is generating and sending data, operators are reacting to that data
148
191
On top of that, some operators may change the timing when events happen. We can illustrate these changes with a visual description called a marble diagram.
149
192
</aside>
150
193
194
+
<!-- v -->
195
+
196
+
## How to Read Marble Diagrams
197
+
198
+
- The name of the operator in in the center
199
+
- Lines above and below represent data moving through time
200
+
- Time travels from left to right
201
+
- Top line = input
202
+
- Bottom line = output
203
+
204
+
<!-- v -->
205
+
206
+
- Symbols on the lines represent data
207
+
- If symbols are different, they mean data is of different types
208
+
209
+
<!-- v -->
210
+
211
+
- An X means an error or exception occurred
212
+
- A vertical bar means the stream terminated normally
213
+
214
+
<!-- > -->
215
+
216
+
## Diagrams + code
217
+
218
+
Whiteboard explanation 🤓
219
+
151
220
<!-- > -->
152
221
153
222
## In Class Activity
@@ -161,12 +230,55 @@ Visit [this website](https://rxmarbles.com) that has marble diagrams for some op
161
230
162
231
<!-- > -->
163
232
233
+
## Subjects
234
+
235
+
A special case of publisher that also adhere to the [Subject protocol](https://developer.apple.com/documentation/combine/subject).
236
+
237
+
Lets you inject values in a stream by calling a `send` method.
238
+
239
+
<asideclass = "notes">
240
+
Subjects can act as a conductor to send values from non-Combine imperative code to Combine subscribers.
PassthroughSubject let's you publish new values on demand. They will pass along values and a completion event.
252
+
253
+
CurrentValueSubject lets you look at the current value of a publisher.
254
+
</aside>
255
+
256
+
<!-- > -->
257
+
258
+
## Playground Challenge
259
+
260
+
Blackjack hand dealer
261
+
262
+
<!-- > -->
263
+
264
+
## Why Combine?
265
+
266
+
Now that you've been introduced to Combine, what's one benefit of using it over "standard code"? 🤔
267
+
268
+
- Write your answer in the chat
269
+
270
+
<!-- > -->
271
+
164
272
## After Class
165
273
274
+
Check out the Subscription protocol, specifically the `request` method. It takes in a value for demand. What is this? Research the meaning of it and how it relates to a concept called **Backpressure**.
275
+
276
+
We saw we can use `Just` to emit single values to a subscriber. Research what `Future` does.
0 commit comments