In this course, you will learn the basics of the Combine framework. This is for absolute beginners in functional reactive programming and Combine š¤©. You can get the full course on Combine here.
Subscribers and Subscriptions
First I will explain how adding a subscriber like sink or assign to a publisher creates a subscription. You will learn how to cancel a subscription that means how to stop a data stream from passing values. You will also learn that passing an error š£ will complete the data stream.
Subscribers and Subscriptions Publishers and Subjects
In the second section, you will learn about a special type of publisher that is Subjects. I will show you how to use CurrentValueSubjects and PassthroughSubjects. In SwiftUI you can also use a publisher with the property wrapper @Published. You will learn the difference between these 3 publishers and their specific use cases.
Example projects for UIkit and SwiftUI
We will also write 2 demo projects š for UIKit and SwiftUI. The goal is to use Subjects and @Published to create data streams from user input. By the end of this course, you will be able to create simple data streams between different parts of your projects.
I am using MVVM to illustrate how easy it is to separate tasks into their own components. Combine is a great tool for loose communication between different components. Comparable to the Delegate pattern, closures, and observer pattern.
Course overview:
0:00 Intro
03:21 subscriptions
17:49 limited subscriptions
31:43 assign(to,on) Subscriber
40:56 assign(to,on) strong reference and memory cycle problems
46:55 assign(to) with @Published properties
52:55 multithreading with .receive(on) and subscribe(on)
01:01:36 subscription as the contract between publishers and subscribers
01:14:18 publishers
01:18:25 CurrentValueSubject
01:26:24 PassthroughSubject
01:39:42 @Published
01:49:25 Combine in SwiftUI
01:55:47 Demo project UIKit
02:12:13 Demo project SwiftUI
02:37:49 where to go from here
Resources
ā¬ļø download playgrounds for the courseĀ hereĀ
š Resources to learn CombineĀ here
How to go from here:
- Ā convenience Publishers: Just, Future, Result, Fail, EmptyĀ https://developer.apple.com/documentation/combineĀ
- Foundation: URLSession dataTask publisher, Notification center, KVO publisher
- operators: map, tryMap, compactMap, scan, collect, append, decodeĀ https://developer.apple.com/documentation/combine/publishers-merge-publisher-operatorsĀ
- throwing errors: Fail publisher, Future with error
- error handling operators: catch, tryCatch, replaceError(with), assertNoFailure , mapError, setFailureType(to)
- multithreading: Schedular protocol, receive(on), subscribe(on), batch processing with flatMap, and set number of tasks executed in parallel with flatMap(maxPublisher)
- control resources: dropFirst, debounce, throttle, filter, removeDupicates
- unit testing: functions that return AnyPublisher, eraseToAnyPublisher, Result.publisher
- communication design patterns can be replaced by Combine data streams: Delegate pattern, closures, and Observer patterns