Home

Awesome

RxSwift to Combine Cheatsheet

This is a Cheatsheet for RxSwift developers interested in Apple's new Combine framework.

It's based on the following blog post: https://medium.com/gett-engineering/rxswift-to-apples-combine-cheat-sheet-e9ce32b14c5b

Basics

RxSwiftCombine
Deployment TargetiOS 8.0+iOS 13.0+
Platforms supportediOS, macOS, tvOS, watchOS, LinuxiOS, macOS, tvOS, watchOS, UIKit for Mac ¹
SpecReactive Extensions (ReactiveX)Reactive Streams (+ adjustments)
Framework ConsumptionThird-partyFirst-party (built-in)
Maintained byOpen-Source / CommunityApple
UI BindingsRxCocoaSwiftUI ²

Core Components

RxSwiftCombineNotes
AnyObserverAnySubscriber
BehaviorRelaySimple wrapper around BehaviorSubject, could be easily recreated in Combine
BehaviorSubjectCurrentValueSubjectThis seems to be the type that holds @State under the hood
Completable
CompositeDisposable
ConnectableObservableTypeConnectablePublisher
DisposableCancellable
DisposeBagA collection of AnyCancellablesCall anyCancellable.store(in: &collection), where collection can be an array, a set, or any other RangeReplaceableCollection
DriverObservableObjectBoth guarantee no failure, but Driver guarantees delivery on Main Thread. In Combine, SwiftUI recreates the entire view hierarachy on the Main Thread, instead.
MaybeOptional.Publisher
ObservablePublisher
ObserverSubscriber
PublishRelaySimple wrapper around PublishSubject, could be easily recreated in Combine
PublishSubjectPassthroughSubject
ReplaySubject
ScheduledDisposable
SchedulerTypeScheduler
SerialDisposable
Signal
SingleDeferred + FutureFuture has to be wrapped in a Deferred, or its greedy as opposed to Single's laziness
SubjectTypeSubject
TestSchedulerThere doesn't seem to be an existing testing scheduler for Combine code

Operators

RxSwiftCombineNotes
amb()
asObservable()eraseToAnyPublisher()
asObserver()
bind(to:)assign(to:on:)Assign uses a KeyPath which is really nice and useful. RxSwift needs a Binder / ObserverType to bind to.
bufferbuffer
catchErrorcatch
catchErrorJustReturnreplaceError(with:)
combineLatestcombineLatest, tryCombineLatest
compactMapcompactMap, tryCompactMap
concatappend, prepend
concatMap
createApple removed AnyPublisher with a closure in Xcode 11 beta 3 :-(
debouncedebounce
debugprint
deferredDeferred
delaydelay
delaySubscription
dematerialize
distinctUntilChangedremoveDuplicates, tryRemoveDuplicates
dohandleEvents
elementAtoutput(at:)
emptyEmpty(completeImmediately: true)
enumerated
errorFail
filterfilter, tryFilter
firstfirst, tryFirst
flatMapflatMap
flatMapFirst
flatMapLatestswitchToLatest
from(optional:)Optional.Publisher(_ output:)
groupBy
ifEmpty(default:)replaceEmpty(with:)
ifEmpty(switchTo:)Could be achieved with composition - replaceEmpty(with: publisher).switchToLatest()
ignoreElementsignoreOutput
interval
justJust
mapmap, tryMap
materialize
mergemerge, tryMerge
merge(maxConcurrent:)flatMap(maxPublishers:)
multicastmulticast
neverEmpty(completeImmediately: false)
observeOnreceive(on:)
ofSequence.publisherpublisher property on any Sequence or you can use Publishers.Sequence(sequence:) directly
publishmakeConnectable
range
reducereduce, tryReduce
refCountautoconnect
repeatElement
retry, retry(3)retry, retry(3)
retryWhen
sample
scanscan, tryScan
shareshareThere’s no replay or scope in Combine. Could be “faked” with multicast.
skip(3)dropFirst(3)
skipUntildrop(untilOutputFrom:)
skipWhiledrop(while:), tryDrop(while:)
startWithprepend
subscribesink
subscribeOnsubscribe(on:)RxSwift uses Schedulers. Combine uses RunLoop, DispatchQueue, and OperationQueue.
take(1)prefix(1)
takeLastlast
takeUntilprefix(untilOutputFrom:)
throttlethrottle
timeouttimeout
timerTimer.publish
toArray()collect()
windowcollect(Publishers.TimeGroupingStrategy)Combine has a TimeGroupingStrategy.byTimeOrCount that could be used as a window.
withLatestFrom
zipzip

Contributing

Add any data/operators to the appropriate CSV files in the Data folder, run bundle install and generate.rb.

Finally, commit the changes and submit a Pull Request.