Created
June 11, 2020 09:08
-
-
Save MathiasSeguy-Android2EE/7d7beb346d9f64dc8710e9e7df4ae235 to your computer and use it in GitHub Desktop.
Markdium-Chapter 9: Observable's action operators
This file contains hidden or 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 observableSrc | |
| * use doOnSubscribe, doOnLifecycle, doOnComplete, doOnDispose, doOnTerminate, | |
| * doAfterTerminate, doFinally, doAfterNext, doOnNext | |
| * to understand its behavior | |
| * This case is the onComplete reached case * | |
| * @return | |
| */ | |
| public static Observable getObservableLifecycleTrackerDoOn() { | |
| /** | |
| * Only trigger the onNext | |
| * Only have the value of the next item | |
| */ | |
| return observableSrc | |
| .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 io.reactivex.internal.operators.observable.ObservableFromArray$FromArrayDisposable@3c09711b | |
| * doOnLifecycle0:doOnSubscribe | |
| * doOnNext:called with Monday | |
| * value emitted is Monday | |
| * doAfterNext:called with Monday | |
| * ... blabla ... | |
| * doAfterNext:called with Sunday | |
| * doOnComplete:called <-onComplete called, onDispose not called | |
| * doOnTerminate:called <-onTerminate called, onDispose not called | |
| * onCompleteCalled <-onComplete called, onDispose not called | |
| * doFinally:called <-always called | |
| * doAfterTerminate:called <-doAfterTerminate called, onDispose not called | |
| * Disposing the observable while it still runs | |
| */ | |
| @Test | |
| public void testObservableLifecycleTrackerDoOn() throws InterruptedException { | |
| Disposable disposable=Answer9_ObservableActions.getObservableLifecycleTrackerDoOn() | |
| .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