Search code examples
rx-swiftcombine

What is difference between Combine and RxSwift?


I wonder if there is an excellent document to show the difference between Combine and RxSwift?

It's good for me to quickly learn the Combine because I already had good knowledge about RxSwift


Solution

  • Đức Bùi posted a good article detailing the surface level differences. In this answer, I'll cover some other differences I've discovered...

    A Cancellable and a Disposable are much more different than the article suggests. The former is required to keep the subscription alive, while the latter is only needed if you want to explicitly kill the subscription. So Disposables can be ignored, but Cancellables never can.

    The share(replay: x) operator in Combine is equivalent to RxSwift's .share(replay: x, scope: .forever) and there is no notion of a share .whileConnected. This means that shared publishers can't be restarted like shared Observables can.

    Other issues:

    • The back-pressure handling in Combine is an extra complication that you will never need, but have to deal with anyway.
    • Every operator in Combine returns a different type and most of them are wrappers around their source type, so they get extremely complex very fast. The eraseToAnyPublisher() operator provides some relief but having to constantly use it is its own torture.
    • The lack of UIKit support is tough. It's better to go with SwiftUI. As such, it's not a simple translation from one API to the other, but a complete rewrite.
    • You will quickly find yourself reaching for third-party libraries in order to fill in the holes that Combine left open. If you are switching to Combine in order to avoid third-party libraries, you've defeated the purpose.

    As for benefits? I have only found one. Combine Subjects are thread safe whereas RxSwift Subjects are not. In RxSwift you rarely need Subjects anyway, in Combine you need them constantly.