Search code examples
reactive-programmingrx-swift

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 ?


Solution

    • 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.