Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxSwift - .subscribe vs .subscribeNext what is the difference?

What is the difference betweeen these two operators ? http://reactivex.io dont mention .subscribeNext at all.

like image 586
Alexey K Avatar asked Nov 02 '16 18:11

Alexey K


People also ask

What is OnNext in Rxswift?

parameter onNext: Action to invoke for each element in the observable sequence. - returns: Subscription object used to unsubscribe from the observable sequence.

What is OnNext?

OnNext. conveys an item that is emitted by the Observable to the observer. OnCompleted. indicates that the Observable has completed successfully and that it will be emitting no further items.


1 Answers

In RxSwift versions older than 3, subscribeNext(_: Value -> ()) was a specialized version of subscribe(_: Event<Value> -> ()).

subscribe(_:) would be triggered for every cases of event, namely .next(Value), .error(Error) and .completed.

subscribeNext would trigger only for .next(Value), unpacking Value first.

As of RxSwift version 3, subscribeNext is now

func subscribe(
  onNext: ((Value) -> ())? = nil,
  onError: ((Error) -> ())? = nil,
  onCompleted: (() -> ())? = nil, 
  onDisposed: () -> () = nil
)

The nil default values enabling users to call subscribe only with the callbacks they are interested about.

like image 167
tomahh Avatar answered Jan 23 '23 23:01

tomahh