Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is difference between do(onNext:) and subscribe(onNext:)?

Tags:

rx-swift

I'm new in RxSwift, I don't understand what is difference between do(onNext:) and subscribe(onNext:).

I google it but did't found good resources to explain the difference.

like image 916
behrad Avatar asked Dec 31 '18 10:12

behrad


1 Answers

At the beginning of a cold Observable chain there is a function that generates events, for e.g. the function that initiates a network request.

That generator function will not be called unless the Observable is subscribed to (and by default, it will be called each time the observable is subscribed to.) So if you add a do(onNext:) to your observable chain, the function will not be called and the action that generates events will not be initiated. You have to add a subscribe(onNext:) for that to happen.

(The actual internals are a bit more complex than the above description, but close enough for this explanation.)

like image 134
Daniel T. Avatar answered Jan 02 '23 03:01

Daniel T.