RxSwift: Requiescat in Pace (RIP)

RxSwift: Requiescat in Pace (RIP)Did Apple just kill RxSwift with its own reactive framework?Michael LongBlockedUnblockFollowFollowingJun 5By now you’re probably at least aware of one of WWDC’s biggest announcements: SwiftUI.

If not, you may want to take a moment to read my take on it, Are You an iOS Ex-Expert?Regardless, what you may not be aware of is that Apple has effectively killed RxSwift at the same time… by baking its successor directly into iOS 13, iPadOS 13, macOS Catalina, watchOS 6, and tvOS 13.

Meet CombineCombine is Apple’s very own reactive framework.

To quote:The Combine framework provides a declarative Swift API for processing values over time.

These values can represent user interface events, network responses, scheduled events, and many other kinds of asynchronous data.

Combine declares publishers to expose values that can change over time, and subscribers to receive those values from the publishers.

Combine also provides operators to manipulate those values, and if you examine the list you’ll see many, many familiar faces.

Filter, map, flatMap, compactMap, combine, merge, concat, zip, reduce, scan, throttle, debounce, timeout, and others.

Plus many more that provide the same functionality, but under a different name, like: collect for toArray, or prepend for startWith, or removeDuplicates for distinctUntilChanged.

And Combine even adds some new ones, like count and measureInterval.

Take a look at the following code:let subject1 = CurrentValueSubject<Int, TypedError>(1)let subject2 = PassthroughSubject<Int, TypedError>()let subscription = subject1 .

combineLatest(subject2) { return $0 + $1 } .

map { "($0)" } .

subscribe(on: ImmediateScheduler.

shared) .

handleEvents(receiveOutput: { value in print(value) }, receiveCompletion: { e in if let case .

failure(error) = e { print("error (error)") } })Look familiar?So is RxSwift dead?That answer is a definite: Yes.

And No.

And Maybe.

For now, no.

Like SwiftUI, Combine requires iOS 13 as a base and as such, it’s not going to replace everything quite yet.

And RxSwift is part and parcel of thousands of existing iOS and macOS projects, so again no… for now.

But maybe yes?This situation is analogous to the time Apple introduced Codable into Swift 4… and in the process pretty much rendered every other third-party JSON coding library obsolete.

Over time, and based on my experience, most apps have dropped those third-party libraries and embraced Codable.

I know that we have here at work, cleaning up our code, eliminating yet another third-party dependency, and reducing technical debt in the process.

Further, SwiftUI has baked Combine into its DNA, as it’s the primary mechanism used to trigger UI changes and updates.

This means that everyone who uses SwiftUI is going to be exposed to Combine.

Are you going to import yet another third-party library when equivalent functionality already exists on your platform?Or just maybe?That said, I’ve yet to see what we would think of as RxCocoa integrations, where Combine is tied into Notifications and Timers and other system services.

Not saying they’re not out there, mind you, just that I haven’t seen them yet.

EDIT: According to the Xcode 11 release notes:The Foundation integration for the Combine framework is unavailable.

The following Foundation and Grand Central Dispatch integrations with Combine are unavailable: KeyValueObserving, NotificationCenter, RunLoop, OperationQueue, Timer, URLSession, DispatchQueue, JSONEncoder, JSONDecoder, PropertyListEncoder, PropertyListDecoder, and the @Publishedproperty wrapper.

Apparently additional system integrations are coming.

Stay tuned.

Cross-Platform CodeAnother entry in the “no” column is that some companies use Rx across multiple platforms in order to better share code and business logic.

Then again, the syntax used and the operations provided are decidedly similar.

Translation to and from Combine wouldn’t be too difficult.

Needless to say, the folks over at the RxSwift repo are well aware of Combine and its implications, and are busy trying to decide how RxSwift fits into this new landscape.

Rebuild Rx on top of Combine?.Provide adaptors, as RxDart does with Flutter?.Simply type alias Observables to Publishers?Only time will tell.

I never bothered to learn that RxSwift stuff!Some who reads this may feel a certain smugness in the fact that they never bothered to learn RxSwift, and now that it’s “on its way out”, will never need to do so.

But while RxSwift may — and I repeat may — be fading off into the sunset, the Reactive principles it inspired and encouraged are definitely NOT fading away.

They’re still out there, gathering steam and moving towards you with all of the subtlety of an oncoming locomotive.

As mentioned above, Combine is front and center in SwiftUI.

It’s the primary and required mechanism by which state changes are communicated to Views.

final class UserData: BindableObject { let didChange = PassthroughSubject<UserData, Never>() var showFavoritesOnly = false { didSet { didChange.

send(self) } } var landmarks = landmarkData { didSet { didChange.

send(self) } }}There’s no escape.

Reactive technologies are in your future, like it or not.

Completion BlockAs Apple says, by adopting Combine, you’ll make your code easier to read and maintain by centralizing your event-processing code and eliminating troublesome techniques like nested closures and convention-based callbacks.

Much as was said about RxSwift.

And as I said in my discussion of SwiftUI, the one thing about computers and technology is that it seemingly advances at light speed.

What you know one day may very well be obsolete the next.

Or not.

Because if you learned RxSwift, the reactive principles you know and love will serve you in good stead going forward.

Combine is not an extinction event.

It’s a vindication.


. More details

Leave a Reply