I'm trying to execute two Maybe at once and call a specific method once both are done. This works if both Observables return a value but in certain cases one might not emit an item thus calling only doOnComplete and not doOnSuccess. So if one of those Maybes' doesn't call doOnSuccess the zip() block isn't executed. I'm wondering how to handle such a scenario?
Following my code (stripped down to the essential part):
private void fetchData(){
Maybe<Integer> maybeOne = getId(); // may return Maybe.empty()
Maybe<List<String>> maybeTwo = getList();
Maybe.zip(maybeOne, maybeTwo, (id, list) -> {
initView(id, list); // only called if values have been emitted
return true;
}).subscribe();
}
I would expect that the zip() block is always called but with null values in case the Maybe didn't call onSuccess. This isn't the case so can I handle such a scenario?
You can use the materialize operator. It basically transforms the serial invocation into object (wrapped inside a Notification object).
Observable.zip(maybeOne.toObservable().materialize(),
maybeTwo.toObservable().materialize(), (id, list) -> {
Log.d(TAG, "zip completed");
return true;
}).subscribe();
Now your zip will always "finish", but your real data can be retrieved using:
id.getValue()
And if your maybe is Maybe.empty() then it will not return the value, but null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With