Alexey K
Alexey K

Reputation: 6723

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

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

Upvotes: 8

Views: 2710

Answers (1)

tomahh
tomahh

Reputation: 13661

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.

Upvotes: 15

Related Questions