Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxCocoa - Why doesn't PublishRelay have an asDriver() method?

On RxCocoa I was wondering why the PublishRelay doesn't have an asDriver() method like the BehaviorRelay ? Currently if I want to convert the publishRelay into a Driver, I have to specify what to return in case of error which seems weird given that the relays can't generate errors...

like image 294
Bioche Avatar asked Jan 29 '19 17:01

Bioche


People also ask

What is PublishRelay?

PublishRelay. This type of Relay will reemit all events once the Observer has subscribed to it.

What is Behaviour relay in RxSwift?

RxSwift also provides a concept called Relays. RxSwift provides two of these, named PublishRelay and BehaviorRelay . These wrap their respective subjects, but only accept and relay next events. You cannot add a completed or error event onto relays at all, so they're great for non-terminating sequences.

What is RxSwift driver?

RxSwift: Driver Go from: a single observable that updates the entire UI to bindTo and reuse the same observable across the viewController.


1 Answers

Those two versions of ...Relay are used to model different concepts:

  • BehaviorRelay represents State
  • PublishRelay represents Events

It makes sense to replay State, hence BehaviorRelay replays its latest value.

It makes less (no?) sense to replay Events, hence PublishRelay does not replay its latest value.

With this in mind, it makes sens for a BehaviorRelay to be transformable to Driver, as a driver drives the application using State. The sharing strategy for BehaviorRelay and Driver is to share side effects and replay the latest value while at least one observable is connected.

A PublishRelay is better represented by a Signal, so you probably could use a Signal to emit to. The sharing strategy in this case is will not replay the latest value, but still share the side effects while at least one observable is connected.

(I build this answer using this great comment from @freak4pc on RxSwift's repository)

like image 196
tomahh Avatar answered Oct 19 '22 23:10

tomahh