kaushikgopal / RxJava-Android-Samples

Learning RxJava for Android by example
Apache License 2.0
7.55k stars 1.37k forks source link

After value change -> execute once #78

Closed Rainer-Lang closed 8 years ago

Rainer-Lang commented 8 years ago

After getting a specfic value from an Observable, I would like to execute a method only once. -> on Completed()

How could this be possible?

WassimBenltaief commented 8 years ago

You can use one of the doOnX methods :

doOnNext() doOnCompleted() doOnError() doOnEach()

In your case it's doOnCompleted() :

Marble diagram

Rainer-Lang commented 8 years ago

@WassimBenltaief So I should complete/end the stream and make within doOnCompleted my method call?

WassimBenltaief commented 8 years ago

If you want to execute the method once the stream ends succesfully so doOnCompleted is the right place. If your expected value is in the middle of the stream so doOnNext with a "check" inside the provided Action is the appropriate.

kaushikgopal commented 8 years ago

@Rainer-Lang @WassimBenltaief is 💯 right.

executing a single method can be done in many ways. it depends at which point you want the single method to be executed. in all likelihood you would use a variant of the doOnX as suggested.

Have a look at the repository to see the different ways doOnNext is used or doOnSubscribe etc.

I'm closing this issue as it has nothing to do with the examples in this repo.

Rainer-Lang commented 8 years ago

@kaushikgopal It wasn't executing a method. It was about executing a method after a change of an Observable to a specific value and THAN execute a method only once and finish. But you're right, @WassimBenltaief gave me a hint. BUT, maybe this could be also something for your repo. ;)