Skip to content

Instantly share code, notes, and snippets.

@MathiasSeguy-Android2EE
Created June 11, 2020 08:31
Show Gist options
  • Select an option

  • Save MathiasSeguy-Android2EE/c69ea53df864f99f1573f9dc22a48e77 to your computer and use it in GitHub Desktop.

Select an option

Save MathiasSeguy-Android2EE/c69ea53df864f99f1573f9dc22a48e77 to your computer and use it in GitHub Desktop.
Markdium-Chapter 9: Observable's action operators
/**
* 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