SECRET OF CSS

Why You Should Migrate From RxSwift To Combine | by Kelvin Tan | Sep, 2022


A brief discussion to help you get rid of your doubts

0*pE4yC7vsDacQhFap 300w
Photo by Goran Ivos on Unsplash

There are many ways to look at it. I like the idea of fewer third-party libraries being used in the project besides Apple’s. Also, both Combine and RxSwift could do the same regarding reactive programming.

Weighing on those, I’ve started migrating from RxSwift to Combine, which is rather straightforward.

Before that, I wanted to take the time to thank the contributors for creating and maintaining RxSwift is a game changer, and it has been a wonderful tool for a lot of us. What each contributor has done is not an easy task. And as more and more opt for Combine, may I suggest another perspective where you can proudly and boldly say Combine was created because you have created/maintained RxSwift?

For that, I thank each of the contributors from the bottom of my heart, and I am sure many others feel the same way.

Let’s now move to Combine, a native framework created by Apple that uses many of the concepts from RxSwift. It is also worth noting that Combine is only supported on iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+.

Using Combine will also make the transition process of using SwiftUI in the future easier.

The key to migrating is understanding what the correct replacement for RxSwift is.

RxSwiftCombineNotesBehaviorSubjectCurrentValueSubjectRequires a default valuePublishSubjectPassthroughSubjectDoes not require any default valueBehaviorRelayN/A

Some examples of codes will help to strengthen our understanding further. This simple implementation shows you how to initialise and emit the value.

// RxSwiftlet isLoading = PublishSubject<Bool>()
let buttonIsLoading = BehaviorSubject<Bool>(false)
..
..
isLoading.onNext(true)
buttonIsLoading.onNext(true)

With Combine, you can choose to emit Error or Never. Below implementation will not emit Error but if you needed to emit Error, simply replace Never to Error.

// Combinelet isLoading = PassthroughSubject<Bool, Never>()
let buttonIsLoading = CurrentValueSubject<Bool, Never>(false)
..
..
isLoading.send(true)
buttonIsLoading.send(true)

Next up, getting the value that is being emitted to display either on our UI

RxSwiftCombineNotessubscribesinkbind(to:)assign(to: on:)Assign uses a KeyPath. RxSwift needs a Binder / ObserverType to bind to.DisposableCancellableDisposeBagAnyCancellables

Following the rest of the codes implemented above, here are some of the ways you can get the values:

If you look at it carefully, the migration process only involves replacing words with words. So, the process is rather easy and quick. However, for additional caution, you will still need to test your code to make sure it works the same.

These tips will get you up and running on migrating your code. Don’t get stuck in the process. The Combine community group has created a cheat sheet to help you identify those keywords. Check them out at GitHub.

Credits go to the Combine community for making the transition easy for all of us.



News Credit

%d bloggers like this: