RxSwift - toant-dev/toandev.github.io GitHub Wiki

Common operations:

  • Throttle
  • Debounce
  • Retry
  • combineLatest
  • observeOn(MainScheduler.instance)
  • PublishSubject

Comparations

map vs flatMap

FlatMap behaves very much like map, the difference is that the function it applies returns an observable itself, so it's perfectly suited to map over asynchronous operations. Summary:

  • Map returns an object of type T
  • FlatMap returns an Observable.

Throttle vs Debounce

Both debounce and throttle are used to filter items emitted by an observable over time.

  • throttle emits only the first item emitted by the source observable in the time window.
  • debounce only emits an item after the specified time period has passed without another item being emitted by the source observable.

zip vs combineLatest

  • zip will take both Observable and Observable and let you combine them into CerealsWithMilk via a provided function. This emits a new CerealsWithMilk each time you get get both a Milk and a Cereals.
  • combineLatest is similar to zip except it will emit a new CerealsWithMilk even if just a new Milk or just a new Cereals is emitted.

merge vs concat

merge can interleave the outputs, while concat will first wait for earlier streams to finish before processing later streams. In your case, with single-element, static streams, it is not making any real difference (but in theory, merge could output words in random order and still be valid according to spec).

Schedulers

  • CurrentThreadScheduler
  • MainScheduler
  • SerialDispatchQueueScheduler
  • ConcurrentDispatchQueueScheduler (background)
  • OperationQueueScheduler (Operation Queue with maxConcurrentOperationCount)

Subjects

  • PublishSubject emits to an observer only those items that are emitted by the source Observable(s) subsequent to the time of the subscription.
  • AsyncSubject emits the last value (and only the last value) emitted by the source Observable, and only after that source Observable completes. (If the source Observable does not emit any values, the AsyncSubject also completes without emitting any values.)
  • BehaviorSubject, it begins by emitting the item most recently emitted by the source Observable (or a seed/default value if none has yet been emitted) and then continues to emit any other items emitted later by the source Observable(s).
  • ReplaySubject emits to any observer all of the items that were emitted by the source Observable(s), regardless of when the observer subscribes.

Hot and Cold Observables

========================

Hot Observables Cold observables
... are sequences ... are sequences
Use resources ("produce heat") no matter if there is any observer subscribed. Don't use resources (don't produce heat) until observer subscribes.
Variables / properties / constants, tap coordinates, mouse coordinates, UI control values, current time Async operations, HTTP Connections, TCP connections, streams
Usually contains ~ N elements Usually contains ~ 1 element
Sequence elements are produced no matter if there is any observer subscribed. Sequence elements are produced only if there is a subscribed observer.
Sequence computation resources are usually shared between all of the subscribed observers. Sequence computation resources are usually allocated per subscribed observer.
Usually stateful Usually stateless

Apple's Combine

Combine is Apple's own implementation of the Reactive Streams standard. The main differences to consider are that Combine uses typed errors, and supports backpressure. Its huge downside is that it's closed source, limited to iOS 13 and up, and does not support Linux as of today.

It also does not have its own testing framework like RxTest or RxBlocking, and doesn't have its own UIKit-supporting framework such as RxCocoa, since it is more aimed towards SwiftUI. Some of these issues have been addressed by community projects such as CombineCocoa, combine-schedulers and CombineExpectations.

⚠️ **GitHub.com Fallback** ⚠️