Skip to content

Commit 09dd355

Browse files
committed
Edit L10
1 parent ed8ab0b commit 09dd355

File tree

4 files changed

+413
-7
lines changed

4 files changed

+413
-7
lines changed

Lessons/Combine-Pt.1/README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ These values can represent many kinds of asynchronous events.
7373
- Has two associated types: one for **Output** and one for **Failure**
7474
- Value types
7575

76+
<aside class="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+
7680
<!-- > -->
7781

7882
## Subscriber
@@ -81,6 +85,13 @@ These values can represent many kinds of asynchronous events.
8185
- Has two associated types: one for **Input** and one for **Failure**
8286
- Reference types (usually act and mutate state upon receipt of values)
8387

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 😎)
94+
8495
<!-- > -->
8596

8697
<img src="https://heckj.github.io/swiftui-notes/images/diagrams/input_output.svg">
@@ -93,13 +104,45 @@ When you connect a subscriber to a publisher, both types must match: Output to I
93104

94105
<!-- > -->
95106

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+
96135
## Operator
97136

98137
- An object that acts both like a subscriber and a publisher
99138
- Classes that adopt both the Subscriber protocol and Publisher protocol
100139
- They support subscribing to a publisher, and sending results to any subscribers
101140
- Can be chained to process, transform data
102141

142+
<aside class="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.
144+
</aside>
145+
103146
<!-- > -->
104147

105148
<img src="https://heckj.github.io/swiftui-notes/images/diagrams/pipeline.svg">
@@ -148,6 +191,32 @@ A publisher is generating and sending data, operators are reacting to that data
148191
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.
149192
</aside>
150193

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+
151220
<!-- > -->
152221

153222
## In Class Activity
@@ -161,12 +230,55 @@ Visit [this website](https://rxmarbles.com) that has marble diagrams for some op
161230

162231
<!-- > -->
163232

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+
<aside class = "notes">
240+
Subjects can act as a conductor to send values from non-Combine imperative code to Combine subscribers.
241+
</aside>
242+
243+
<!-- > -->
244+
245+
## Built-in Subjects
246+
247+
- [PassthroughSubject](https://heckj.github.io/swiftui-notes/#reference-passthroughsubject)
248+
- [CurrentValueSubject](https://heckj.github.io/swiftui-notes/#reference-currentvaluesubject)
249+
250+
<aside class ="notes">
251+
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+
164272
## After Class
165273

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.
166277

167278
<!-- > -->
168279

169280
## Additional Resources
170281

171282
- [Using Combine](https://heckj.github.io/swiftui-notes/#coreconcepts-publisher-subscriber)
172283
- Book: Practical Combine by Donny Walls
284+
- Book: Combine - Asynchronous programming with Swift By Shai Mishali, Marin Todorov, Florent Pillet and Scott Gardner

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ Students by the end of the course will be able to ...
4141
| 6 | Wed, Feb 10 | [Functional Programming Pt.1] |
4242
| 8 | Mon, Feb 15 | [Functional Programming Pt.2] |
4343
| 9 | Wed, Feb 17 | Lab - Sharing Progress |
44-
| 10 | Mon, Feb 22 | [Reactive Programming Pt.1] |
45-
| 11 | Wed, Feb 24 | [Reactive Programming Pt.2] |
46-
| 12 | Mon, Mar 1 | [Reactive Programming Pt.3] |
44+
| 10 | Mon, Feb 22 | [Combine Pt.1] |
45+
| 11 | Wed, Feb 24 | Combine Pt.2 |
46+
| 12 | Mon, Mar 1 | Combine Pt.3 & Review |
4747
| 13 | Wed, Mar 3 | Final Exam |
4848

4949
[Creational Patterns Pt.1]: Lessons/01-Creational-PatternsPt.1/README.md
@@ -56,9 +56,7 @@ Students by the end of the course will be able to ...
5656
[Coordinators]: Lessons/08-Coordinators/README.md
5757
[Functional Programming Pt.1]: Lessons/09-Functional-ProgrammingPt.1/README.md
5858
[Functional Programming Pt.2]: Lessons/10-Functional-ProgrammingPt.2/README.md
59-
[Reactive Programming Pt.1]: Lessons/11-Reactive-ProgrammingPt.1/README.md
60-
[Reactive Programming Pt.2]: Lessons/12-Reactive-ProgrammingPt.2/README.md
61-
[Reactive Programming Pt.3]: Lessons/13-Reactive-ProgrammingPt.3/README.md
59+
[Combine Pt.1]: Lessons/Combine-Pt.1/README.md
6260

6361
## Class Assignments
6462

0 commit comments

Comments
 (0)