1. Introduction
1.介绍
In this tutorial, we’ll be looking at the Maybe<T> type in RxJava – which represents a stream which can emit a single value, complete in an empty state or report an error.
在本教程中,我们将研究RxJava中的Maybe<T>类型–它表示一个流,可以发出一个单一的值,在空的状态下完成,或者报告一个错误。
2. The Maybe Type
2.也许型
Maybe is a special kind of Observable which can only emit zero or one item, and report an error if the computation fails at some point.
Maybe是一种特殊的Observable,它只能发出零或一个项目,如果计算在某一点上失败,就会报告错误。
In this regard, it’s like a union of Single and Completable. All these reduced types, including Maybe, offer a subset of the Flowable operators. This means we can work with Maybe like a Flowable as long as the operation makes sense for 0 or 1 items.
在这方面,它就像Single和Completable的联合。所有这些缩小的类型,包括Maybe,提供了Flowable操作符的子集。这意味着我们可以像处理Maybe一样处理Flowable,只要该操作对0或1项有意义。
Since it can only emit one value, it doesn’t support backpressure handling as with a Flowable:
因为它只能发射一个值,所以它不支持像Flowable那样的背压处理。
Maybe.just(1)
.map(x -> x + 7)
.filter(x -> x > 0)
.test()
.assertResult(8);
There are onSuccess, onError and onComplete signals that can be subscribed from a Maybe source:
有onSuccess、onError和onComplete信号,可以从Maybe源订阅。
Maybe.just(1)
.subscribe(
x -> System.out.print("Emitted item: " + x),
ex -> System.out.println("Error: " + ex.getMessage()),
() -> System.out.println("Completed. No items.")
);
The above code will print Emitted item: 1 since this source will emit a success value.
上面的代码将打印发射的项目。1,因为这个源将发出一个成功的值。
For the same subscriptions:
对于同样的订阅。
- Maybe.empty().subscribe(…) will print “Completed. No items.”
- Maybe.error(new Exception(“error”)).subscribe(…) will print “Error: error”
These events are mutually exclusive for Maybe. That is, onComplete won’t be invoked after onSuccess. This is slightly different from Flowable since onComplete will be invoked when the stream is complete even after possibly some onNext invocations.
这些事件对于Maybe来说是相互排斥的。也就是说,onComplete不会在onSuccess之后被调用。这与Flowable略有不同,因为onComplete将在流完成时被调用,即使在可能有一些onNext调用之后。
Single doesn’t have onComplete signal like Maybe because it’s meant to capture a reactive pattern which can either emit one item or fail.
Single不像Maybe那样有onComplete信号,因为它是为了捕捉一个反应式模式,它可以发出一个项目或者失败。
On the other hand, Completable lacks onSuccess since it’s meant to deal with complete/fail situations only.
另一方面,Completable缺乏onSuccess,因为它只是为了处理完全/失败的情况。
Another use case for the Maybe type is using it in combination with Flowable. The firstElement() method can be used to create Maybe from Flowable:
Maybe类型的另一个用例是将其与Flowable结合使用。firstElement()方法可以被用来从Flowable中创建Maybe。
Flowable<String> visitors = ...
visitors
.skip(1000)
.firstElement()
.subscribe(
v -> System.out.println("1000th visitor: " + v + " won the prize"),
ex -> System.out.print("Error: " + ex.getMessage()),
() -> System.out.print("We need more marketing"));
4. Conclusion
4.结论
In this short tutorial, we quickly looked on RxJava Maybe<T> usage, and how it relates to other reactive types like Flowable, Single and Completable.
在这个简短的教程中,我们迅速了解了RxJava Maybe<T>的用法,以及它与其他反应式类型如Flowable、Single和Completable的关系。
As always code samples can be found over on GitHub.
像往常一样,代码样本可以在GitHub上找到over。