RxJava study notes <thirteen> startWith operator

StartWith takes a sequence and concatenates it before applying to the observable sequence.

output:

StartWith is the shortened name for processing our sequence with concat and just.

concurrent sequence

Observables don't always emit values ​​at predictable moments. Now, we'll see some operators for combining sequences that emit values ​​at the same time.

with

amb takes multiple observables and returns the one that emits the value first, the rest are discarded.

In the example below, amb will mirror the second observable because it waits less time to start.

output:

Its usefulness may not be obvious.

The amb feature is useful if you have multiple cheap resources that can provide value, but the latency is widely variable. For example, you may have servers replicated all over the world. It is cheap to issue queries for both client send and server responses, however, latency is unpredictable and highly variable due to network conditions. Using the Amb operator, you can send the same request to many servers and use the result of the first response.

Another way to do AMB is the ambWith ​​operator, ambWith ​​allows you to combine observables one by one in a chain. This is more convenient when using amb in the middle of a chain or operator.

output:

 

Original link:

https://github.com/Froussios/Intro-To-RxJava/blob/master/Part%203%20-%20Taming%20the%20sequence/4.%20Combining%20sequences.md

If you have anything to discuss, you can add my WeChat public account:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325816263&siteId=291194637