반응형
원문 : https://medium.com/gett-engineering/rxswift-to-apples-combine-cheat-sheet-e9ce32b14c5b
Basics
| RxSwift | Combine | |
| Deployment Target | iOS 8.0+ | iOS 13.0+ |
| Platforms supported | iOS, macOS, tvOS, watchOS, Linux | iOS, macOS, tvOS, watchOS, UIKit for Mac |
| Spec | Reactive Extensions (ReactiveX) | Reactive Streams (+ adjustments) |
| Framework Consumption | Third-party | First-party (built-in) |
| Maintained by | Open-Source / Community | Apple |
| UI Bindings | RxCocoa | SwiftUI |
Core Components
| RxSwift | Combine | Note |
| AnyObserver | AnySubscriber | |
| BehaviorRelay | ❌ | Simple wrapper around BehaviorSubject, could be easily recreated in Combine |
| BehaviorSubject | CurrentValueSubject | This seems to be the type that holds @State under the hood |
| Completable | ❌ | |
| CompositeDisposable | ❌ | |
| ConnectableObservableType | ConnectablePublisher | |
| Disposable | Cancellable | |
| DisposeBag | A collection of AnyCancellables | Call anyCancellable.store(in: collection), where collection can be an array, a set, or any other RangeReplaceableCollection |
| Driver | BindableObject (SwiftUI) | Both guarantee no failure, but Driver guarantees delivery on Main Thread. In Combine, SwiftUI recreates the entire view hierarachy on the Main Thread, instead. |
| Maybe | Publishers.Optional | |
| Observable | Publisher | |
| Observer | Subscriber | |
| PublishRelay | ❌ | Simple wrapper around PublishSubject, could be easily recreated in Combine |
| PublishSubject | PassthroughSubject | |
| ReplaySubject | ❌ | |
| ScheduledDisposable | ❌ | |
| SchedulerType | Scheduler | |
| SerialDisposable | ❌ | |
| Signal | ❌ | |
| Single | Future | They're only similar in the sense of single emission, but Future shares resources and executes immediately (very strange behavior) |
| SubjectType | Subject | |
| TestScheduler | ❌ | There doesn't seem to be an existing testing scheduler for Combine code |
Operators
| RxSwift | Combine | Notes |
| 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. |
| buffer | buffer | |
| catchError | catch | |
| catchErrorJustReturn | replaceError(with:) | |
| combineLatest | combineLatest, tryCombineLatest | |
| compactMap | compactMap, tryCompactMap | |
| concat | append, prepend | |
| concatMap | ❌ | |
| create | ❌ | Apple removed AnyPublisher with a closure in Xcode 11 beta 3 :-( |
| debounce | debounce | |
| debug | ||
| deferred | Publishers.Deferred | |
| delay | delay | |
| delaySubscription | ❌ | |
| dematerialize | ❌ | |
| distinctUntilChanged | removeDuplicates, tryRemoveDuplicates | |
| do | handleEvents | |
| elementAt | output(at:) | |
| empty | Publishers.Empty(completeImmediately: true) | |
| enumerated | ❌ | |
| error | Publishers.Fail | |
| filter | filter, tryFilter | |
| first | first, tryFirst | |
| flatMap | flatMap | |
| flatMapFirst | ❌ | |
| flatMapLatest | switchToLatest | |
| from | ❌ | |
| groupBy | ❌ | |
| ifEmpty(default:) | replaceEmpty(with:) | |
| ifEmpty(switchTo:) | ❌ | Could be achieved with composition - replaceEmpty(with: publisher).switchToLatest() |
| ignoreElements | ignoreOutput | |
| interval | ❌ | |
| just | Just | |
| map | map, tryMap | |
| materialize | ❌ | |
| merge | merge, tryMerge | |
| merge(maxConcurrent:) | flatMap(maxPublishers:) | |
| multicast | multicast | |
| never | Publishers.Empty(completeImmediately: false) | |
| observeOn | receive(on:) | |
| of | Sequence.publisher | `publisher` property on any `Sequence` or you can use `Publishers.Sequence(sequence:)` directly |
| publish | makeConnectable | |
| range | ❌ | |
| reduce | reduce, tryReduce | |
| refCount | autoconnect | |
| repeatElement | ❌ | |
| retry, retry(3) | retry, retry(3) | |
| retryWhen | ❌ | |
| sample | ❌ | |
| scan | scan, tryScan | |
| share | share | There’s no replay or scope in Combine. Could be “faked” with multicast. |
| skip(3) | dropFirst(3) | |
| skipUntil | drop(untilOutputFrom:) | |
| skipWhile | drop(while:), tryDrop(while:) | |
| startWith | prepend | |
| subscribe | sink | |
| subscribeOn | subscribe(on:) | RxSwift uses Schedulers. Combine uses RunLoop, DispatchQueue, and OperationQueue. |
| takeLast | last | |
| takeUntil | prefix(untilOutputFrom:) | |
| throttle | throttle | |
| timeout | timeout | |
| timer | Timer.publish | |
| toArray() | collect() | |
| window | collect(Publishers.TimeGroupingStrategy) | Combine has a TimeGroupingStrategy.byTimeOrCount that could be used as a window. |
| withLatestFrom | ❌ | |
| zip | zip |
반응형
'Dev Study > iOS' 카테고리의 다른 글
| 아이폰 잠금(비밀번호 설정) 여부 확인 (0) | 2022.02.09 |
|---|---|
| 앱 실행시 슬립모드 방지 (0) | 2022.02.09 |
| iOS 버젼별 호환 가능한 iPhone 모델 (0) | 2021.09.13 |
| MVVM with Combine Tutorial for iOS (1) | 2019.09.05 |
| Grand Centrial Dispatch Tutorial for Swift 4: Part 2/2 (0) | 2019.03.18 |
| Grand Centrial Dispatch Tutorial for Swift 4: Part ½ (0) | 2019.03.18 |
| Swift 프레임워크로 코드 재사용하기(Reusing code with Swift frameworks) (0) | 2019.01.29 |
| HealthKit Tutorial With Swift: Workouts (0) | 2019.01.25 |

