RxJava study notes <12> Combining sequences

Combining sequences


So far, we've seen most of the methods that allow us to create a sequence and convert it to the sequence we want. However, most applications will have multiple input sources. We need a way to combine sequences. We've seen some sequences use multiple observables. In this chapter, we will see the most important operators for generating a sequence from multiple sequences.

Concatenation

The most direct sequence combination is one after the other.

concat

The Conat operator concatenates sequences one after the other. There are many overloaded methods, which allow you to provide source observables in different quantities and formats.

 

Concatenating two (or more) given observables is trivial.

output:

If the number of sequences to be combined is dynamic, you can provide an observable that outputs the sequences to be concatenated. In this example, we'll use the familiar groupBy to create a sequence of words starting with the same letter.

output

Conat behaves like ConatMap. Actually, ConatMap is connat after map.

The ConatWith operator is another style of performing conat that allows you to combine sequences one after the other in a chain:

output:

repeat

repeat allows you to concatenate after the sequence itself, either for an infinite or finite amount of time. repeat does not cache values ​​to repeat them. When the time is right, it will start a new subscription and process the old one.

It is also very convenient to write

output:

repeatWhen

If you need more control than repeat gives, you can use the repeatWhen operator to control when replication starts. Time is defined by the observable you provide. When the original sequence completes, it waits for the observable to be processed to emit something (the value is irrelevant), and only then does it repeat. If the observable processing terminates, this means that the repetition should stop.

It can be useful for signals to know when the repetition is done. repeatWhen provides a special observable that signals an invalid when the repetition ends. You can use this observed to construct your signal.

The parameter of RepeatWhen is a function that takes an observable and returns an observable. It doesn't matter what type the two objects emit. The input is an observable representing the end of the repetition, the returned observable will be used to signal a restart.

In the next example we will use repeatWhen to create repeat(n)

output:

Here, the repetition happens immediately: the OB is emitted at the end of the repetition, so the returned observable is also emitted as soon as the repetition is done. This marks the beginning of a new repetition.

In the next example, we create a sequence that repeats every two seconds, forever.

Note that the sequence repeats every 2 seconds regardless of when it finishes. This is because we create a separate interval that sends a signal every 2 seconds. In the next chapter, [Time-shifted sequences](/Part 3 - Taming the sequence/5. Time-shifted sequences.md), we'll see how sequences can be processed in time with more control.

Another thing to watch out for is the ob.subscribe() statement, which seems useless, but is necessary because it forces the ob to be created. In the current implementation, if the ob is not subscribed, the repetition will never start.

 

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=325272415&siteId=291194637