Created
June 11, 2020 09:08
-
-
Save MathiasSeguy-Android2EE/ca083249c09e8076e556fce6bfdf804c to your computer and use it in GitHub Desktop.
Markdium-Chapter 9: Observable's action operators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Using the observableIntervalSrc | |
* use doOnSubscribe, doOnLifecycle, doOnComplete, doOnDispose, doOnTerminate, | |
* doAfterTerminate, doFinally, doAfterNext, doOnNext | |
* to understand its behavior | |
* This case is the onDispose reached case * | |
* @return | |
*/ | |
public static Observable getObservableLifecycleTrackerDoOnWithInterval() { | |
/** | |
* Only trigger the onNext | |
* Only have the value of the next item | |
*/ | |
return observableIntervalSrc | |
.doOnSubscribe(disp -> System.out.println("doOnSubscribe:called with "+disp)) | |
.doOnLifecycle( | |
disposable -> System.out.println("doOnLifecycle0:doOnSubscribe"),//The action to do in onSubscribe | |
() -> System.out.println("doOnLifecycle1:doOnDispose"))//the action to do in onDispose | |
.doOnComplete(() -> System.out.println("doOnComplete:called")) | |
.doOnDispose(() -> System.out.println("doOnDispose:called")) | |
.doOnTerminate(() -> System.out.println("doOnTerminate:called")) | |
.doAfterTerminate(() -> System.out.println("doAfterTerminate:called")) | |
.doFinally(() -> System.out.println("doFinally:called")) | |
.doAfterNext(it -> System.out.println("doAfterNext:called with "+it)) | |
.doOnNext(it -> System.out.println("doOnNext:called with "+it)) | |
; | |
} | |
/** | |
* OutPut result: | |
* doOnSubscribe:called with null | |
* doOnLifecycle0:doOnSubscribe | |
* doOnNext:called with 0 | |
* value emitted is 0 | |
* doAfterNext:called with 0 | |
* doOnNext:called with 1 | |
* value emitted is 1 | |
* doAfterNext:called with 1 | |
* Disposing the observable while it still runs | |
* doOnDispose:called <-onDispose called, onComplete, onTerminate, doAfterTerminate not called | |
* doOnLifecycle1:doOnDispose | |
* doFinally:called | |
*/ | |
@Test | |
public void testObservableLifecycleTrackerDoOnWithInterval() throws InterruptedException { | |
Disposable disposable=Answer9_ObservableActions.getObservableLifecycleTrackerDoOnWithInterval() | |
.subscribe(it -> System.out.println("value emitted is "+it), | |
Throwable::printStackTrace, | |
()->System.out.println("onCompleteCalled")); | |
Thread.sleep(2500); | |
System.out.println( "Disposing the observable while it still runs"); | |
disposable.dispose(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment