Introduction to Combine for iOS developers with RxSwift experience
How to start using Combine for reactive programming
08 Apr 2024 · 4 min read
If you are already exprerienced with RxSwift, switching to Combine will mostly be a matter of learning new namings for similar concepts.
Let's directly jump in.

Publisher & Subscriber
A Publisher is what in RxSwift is an Observable. A publisher defines how values are produced and allows registration of a subscriber.
A Subscriber is what in RxSwift is an Observer. A subscriber receives values from the publisher after subscribing.
Let's remind how to create and subscribe to an Observable in RxSwift first:
let observable = Observable.from([1, 2, 3, 4])observable.subscribe(onNext: { value inprint(value)}, onCompleted: {print("completed")})
And here how it looks to create and register to a Publisher in Combine:
let publisher = Publishers.Sequence<[Int], Error>(sequence: [1, 2, 3, 4])publisher.sink(receiveCompletion: { _ inprint("completed")}) { value inprint(value)}
Both examples produce the same output:
1234completed
SubjectType
A SubjectType is what in RxSwift is a Subject, so the name is not that different in this case. Only the subtypes of a subject were named differently.
There are two kinds of a subject type in Combine: PassthroughSubject and CurrentValueSubject.
A PassthroughSubject is what in RxSwift is a PublishSubject. If you subscribe to it you will get all events that happen after you subscribed.
A CurrentValueSubject is what in RxSwift is a BehaviourSubject. It behaves like a PassthroughSubject but it also will send it's most recent element to new subscribers.
In RxSwift, creating a subject and sending values to the observer might look as follows:
let subject = PublishSubject<String>()subject.asObservable().subscribe(onNext: { value inprint(value)})subject.onNext("Yes")subject.onNext("Ve")subject.onNext("gan")
The equivalent in Combine:
let subject = PassthroughSubject<String, Never>()subject.eraseToAnyPublisher().sink { value inprint(value)}subject.send("Yes")subject.send("Ve")subject.send("gan")
Both examples produce the same output:
YesVegan
If you like going deeper, you can find a good comparison of available RxSwift und Combine components here.
Operators
Many operators like map, flatMap, reduce etc. that are available in RxSwift are also available in Combine. Using Combine operators is just like using RxSwift operators.
So it should feel really familiar to see Combine operators in action:
Publishers.Sequence(sequence: [1, 2, 2, 3, 3, 4, 7]).map { $0 * 2 }.filter { $0.isMultiple(of: 2) }.dropFirst(3).removeDuplicates().sink { value inprint(value)}
This code produces the following output:
6814
A full comparison list of RxSwift und Combine operators is available here.
Error Handling
A notable difference between RxSwift’s Observable and Combine’s Publisher is the error handling. In RxSwift, we work with the general Error type whereas in Combine we can use any kind of custom error:
let publisher = Publishers.Sequence<[Int], SomeCustomError>(sequence: [1, 2])
If a subject can never fail, Combine provides the Never type:
let subject = PassthroughSubject<String, Never>()
Memory Management
To deal with ARC and memory management, RxSwift provides a DisposeBag. In Combine, we can use a collection of AnyCancellables.
Here is an example of how a DisposeBag is typically used in RxSwift:
let disposeBag = DisposeBag()let subject = PublishSubject<String>()subject.asObservable().subscribe(onNext: { value inprint(value)}).disposed(by: disposeBag)
And here is the equivalent example in Combine:
var cancellables = Set<AnyCancellable>()let subject = PassthroughSubject<String, Never>()subject.eraseToAnyPublisher().sink { value inprint(value)}.store(in: &cancellables)
Performance
I didn't compare the performance of RxSwift and Combine myself but there are some great articles about it. They all agree that Combine is clearly faster than RxSwift.
If you are interested in more specifics, you can find some performance tests and its results here. The tests compare the performance of the most commonly used components and operators in RxSwift and Combine.



Newsletter
Like to support my work?
Say hi
Related tags
Articles with related topics
Latest articles and tips



