swiftyplace site icon

Start learning the Combine framework

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.

Play Video

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


⬇️ 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


Subscribe To My Newsletter

Table of Contents

Leave a Reply

Your email address will not be published. Required fields are marked *

More Posts