The author blog

http://www.jianshu.com/u/c50b715ccaeb

preface

In the last section, we mentioned Flowable and Backpressure. Originally, we wanted to talk about these two things, but in the middle of the writing, we still felt that it was not the right time. Therefore, let’s make preparations first. First, I will take you to learn the zip operator. This operator is also quite awesome and involves many things, mainly some details. By learning this operator, we can pave the way for the next section of Backpressure.

To the chase

As usual, let’s stick with a more formal explanation.


Zip uses a function to combine events sent by multiple Observables, and then sends the combined events. It applies this function in a strict order. It emits as much data as the Observable that emits the fewest data items.


Let’s use a simple picture to explain:

As you can see from this diagram, what’s different about upstream this time is that we have two pipes.


One pipe is responsible for sending circular events, and the other pipe is responsible for sending triangular events. By using the Zip operator, the circular and triangular events are combined into a single rectangular event.


Now let’s look at the breakdown action:

By breaking down the actions we can see:


  • Combination process is respectively from two to remove an event in each pipe combination, and an event can only be used once, the order of the combination is in strict accordance with the send smoothly for the event, that is to say there will not be A round 1 and triangle B events to merge, it is impossible to appear round 2 and triangle A merger.


  • Finally, the number of events received downstream is the same as the number of events sent by the least upstream pipe. This also makes sense, because if you take one event from each pipe and merge it, the least one will be the first to finish, at which point the other pipes, although they still have events, don’t have enough events to combine, so downstream doesn’t receive any remaining events.


After analyzing the general principle, we still combine work and rest, first let’s see how to write the actual code:


We created two upstream pipes, one to send 1,2,3,4,Complete, and the other to send A,B,C,Complete. Then we used Zip to combine the sent events.

It seems to be right… But something feels wrong…

What’s wrong? Why does it feel like hose one was sent, and hose two was sent? Is it true? Let’s find out:



This time we added a one-second delay after each event. Check out the result. Note that this is a GIF:

(Dear me, I am afraid you can not see clearly, specially adjusted to the elderly font)


Ah, it seems that it is really the water pipe sent first and again sent water pipe two, why would there be such a situation? Since both pipes are running in the same thread, there must be a sequence of code execution in the same thread.


So let’s change it a little bit, so they don’t have to be on the same thread.



Ok, this time we let the pipes send events in the IO thread, and let’s see the result:

GIF graph:

Either! That’s right. Both pipes start sending at the same time, and as they send one, the Zip combines one and sends the result downstream.


Wrong! Maybe a more careful friend can see the clue, the first pipe clearly sent four data + a Complete, clearly there was before, why not here?


This is because as we said before, the number of events sent by zip is related to the number of events sent by the upstream pipe with the least number of events. In this example, we sent only three events from the second pipe and then sent Complete. At this point, although the first pipe still had event 4 and event Complete, But what’s the point of sending them or not? So in the spirit of thrift is a virtue, simply break his dog legs, stop it.


As for why the previous example will be sent, I have just said yes! In! The same! A!!! A!!!! Line! Cheng! ! !!!! Ask me again and I’ll kill you!


Programmers with good intentions may ask again, what if I don’t send Complete? The obvious answer is that upstream will continue to send events, but downstream will still not receive those extra events. Try it if you don’t believe me.


practice

Zip can be used in many scenarios on Android. Let me give you an example.


For example, if an interface needs to display some user information from two server interfaces, and the information can only be displayed when both are obtained, then Zip can be used:


Start by defining the two request interfaces separately:

Zip is then used to package the request:

Other related articles


RxJava2.0 tutorial for beginners (I) : basic working principles

RxJava2.0 tutorial for beginners (ii) : powerful thread control

RxJava2.0 tutorial for beginners (iii) : map and flatMap operators