iTranslated by AI
Getting Started with Combine: Beginning with Subject
I have written a book titled "Let's Start Combine" for those learning Combine for the first time. In this article, I will write some supplementary content (or perhaps behind-the-scenes stories) regarding that book.
Lowering the Hurdle to Start Combine
Combine is a framework for reactive programming in Swift. It can be used to handle various asynchronous events. Similar frameworks include RxSwift and ReactiveSwift. Since Combine was added as one of the standard frameworks, its adoption is expected to increase in the future.
Reactive programming has an image of having a high hurdle and high learning costs. However, it is actually not that difficult. Therefore, in the aforementioned book, I wanted to lower the hurdle to start with Combine.
In the beginning, I wrote the following as a learning policy:
For a programmer, rather than explaining things with many words, it is faster to understand by actually writing and running the code.
Based on this idea, I prepared code that can be run immediately in Xcode Playground. The goal is to lower the hurdle by making it runnable right away.
import Combine
let subject = PassthroughSubject<String, Never>()
subject
.sink { value in
print("Received value:", value)
}
subject.send("A")
subject.send("B")
subject.send("C")
I believe it is faster to learn in the order of first running the code, and then understanding its meaning.
Adopting Subject for the Publisher
In Combine, something that sends asynchronous events is called a Publisher. Knowing the Publisher is the first step to knowing Combine. In the aforementioned book, I adopted PassthroughSubject as the first Publisher. This approach is not often seen in other explanations. I will explain why I adopted the Subject.
One of the Publishers frequently used in Combine explanations is the NotificationCenter Publisher.
let myNotification = Notification.Name("MyNotification")
let publisher =
NotificationCenter.default.publisher(for: myNotification)
publisher
.sink { value in
print("Received value:", value)
}
NotificationCenter is known by many people and has the advantage of being easy to understand as an introduction. Also, it is convenient because the publisher method is provided by default. On the other hand, it has a drawback: it can only generate simple events.
There are three types of Combine events:
- Value
- Event completion (
.finished) - Error (
.failure)
I believe it is easier to learn if these can be generated freely.
Therefore, I will give an example of a Subject.
let subject = PassthroughSubject<String, MyError>()
subject.send("A")
subject.send(completion: .finished)
subject.send(completion: .failure(.failed))
enum MyError: Error {
case failed
}
The advantage of the Subject is that these three types of events can be easily generated. This advantage aligns with the policy of learning by writing code in Xcode Playground. For that reason, I adopted the Subject.
Points of Caution with Subject
I have stated the reasons for adopting Subject, but there are also points that require caution.
In general, a Publisher is something that only has events flowing from it. However, a Subject can intentionally generate events using send. While this is a convenient feature, it also means it possesses an additional role beyond that of a typical Publisher.
This point is why Subject is often considered a poor example of a Publisher and why it isn't adopted as an introduction to Publishers in other Combine explanations. In the aforementioned book, I prioritized the policy of learning by writing code in Xcode Playground, so I turned a blind eye to this point.
That being said, Subjects are frequently used in actual app development.
There is a way to present a Subject as a simple Publisher.
let subject = PassthroughSubject<String, Never>()
let publisher = subject.eraseToAnyPublisher()
By performing type erasure, you can present it as an AnyPublisher type. You use the subject on the event-sending side and have the event-receiving side use the publisher. In actual app design, it is preferable to utilize this method.
Summary
To learn Combine, utilizing Xcode Playground is effective. Subjects are well-suited for learning within Xcode Playground. However, please be aware that there are differences compared to general Publishers.
This article is a modified version of the talk "Starting Combine with Subject" given at Mobile Act ONLINE #2.
Discussion