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 observableSrc we use doOnNext to understand its behavior | |
*/ | |
public static Observable getObservableDoOnEach() { | |
return observableSrc | |
.doOnNext(it -> System.out.println("Size is " + it.length())); | |
} | |
/** | |
* OutPut result: | |
* doOnNext Size of Monday is 6 |
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 observableWithErrorSrc skip error items and return a bad day item instead of the error | |
* @return | |
*/ | |
public static Observable getObservableWithOnErrorReturnItem() { | |
//you can also return an empty Observable and then quit the Observable nicely | |
return observableWithErrorSrc | |
.onErrorReturnItem("Bad day"); | |
} |
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 observableWithErrorSrc skip error items and return a bad day item instead of the error | |
* @return | |
*/ | |
public static Observable getObservableWithOnErrorReturn() { | |
return observableWithErrorSrc | |
.onErrorReturn(th->"Bad day"); | |
} | |
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 observableSrc we use doOnEach to understand its behavior | |
*/ | |
public static Observable getObservableDoOnEach() { | |
return observableSrc.doOnEach(it -> | |
{ | |
System.out.print("onEach is " + it+" :"); | |
if(it.isOnNext()) { | |
System.out.println("isOnNext = " + it.isOnNext() +" and getValue = " + it.getValue()); | |
}else if(it.isOnError()) { |
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 observableWithErrorSrc skip error items and return an empty observable | |
* @return | |
*/ | |
public static Observable getObservableWithOnErrorResumeNextEmpty() { | |
//you can also return an empty Observable and then quit the Observable nicely | |
return observableWithErrorSrc | |
.onErrorResumeNext(Observable.empty()); | |
} |
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() { | |
/** |
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
* OutPut result: | |
* value emitted is Monday | |
* onCompleteCalled | |
*/ | |
@Test | |
public void testObservableWithOnErrorResumeNextEmpty() { | |
Answer9_ObservableActions.getObservableWithOnErrorResumeNextEmpty() | |
.subscribe(it -> System.out.println("value emitted is " + it), | |
Throwable::printStackTrace, |
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
private static final Observable observableSrc = Observable.just( | |
"Monday", | |
"Tuesday", | |
"Wednesday", | |
"Thursday", | |
"Friday", | |
"Saturday", | |
"Sunday" | |
); |
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 observableWithErrorSrc skip error items and retry twice | |
* @return | |
*/ | |
public static Observable getObservableWithOnErrorRetry() { | |
//you can also return an empty Observable and then quit the Observable nicely | |
return observableWithErrorSrc | |
.retry(2); | |
} | |
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 observableWithErrorSrc skip error and return twice "Buggy Day" | |
* @return | |
*/ | |
public static Observable getObservableWithOnErrorResumeNext() { | |
return observableWithErrorSrc | |
.onErrorResumeNext(Observable.just("Buggy day").repeat(2)); | |
} | |
/** |
NewerOlder