Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between PublishSubject and PublishRelay in RxSwift?

I am new to RxSwift programming. I am confused between the two while coding. Which one should be used to store datasource of table and how to decide that ?

like image 933
Emtiyaj Ali Avatar asked Feb 26 '20 17:02

Emtiyaj Ali


People also ask

What is RxSwift PublishRelay?

import RxSwift. /// PublishRelay is a wrapper for `PublishSubject`. /// /// Unlike `PublishSubject` it can't terminate with error or completed. public final class PublishRelay<Element>: ObservableType {

What is RxSwift PublishSubject?

RxSwift: Publish SubjectStarts empty and only emits new elements to subscribers. Useful when you want subscribers to be notified of new events from the point at which they subscribed until they either unsubscribe or termination ( . completed / . error ).

What is the BehaviorSubject in RxSwift?

BehaviorSubject : Starts with an initial value and replays it or the latest element to new subscribers. ReplaySubject : Initialized with a buffer size and will maintain a buffer of elements up to that size and replay it to new subscribers.


1 Answers

  • A PublishSubject can emit an error or completed event while a PublishRelay cannot.
  • A PublishSubject conforms to the ObserverType protocol while the PublishRelay does not.

Another important point that was alluded to by @RobMayoff in his comment. Neither a PublishSubject nor a PublishRelay stores state, so neither of them are a good idea to "store datasource of table".

Fortunately, you don't need to store the state yourself because the DataSource object that the items operator creates internally stores it.

In other words, you don't need to use a Subject or Relay (of any sort) to feed a table view. Just use an Observable.

like image 189
Daniel T. Avatar answered Oct 06 '22 18:10

Daniel T.