Rxjava2 configuration and usage details

Rxjava 2.0 is a very good stream programming. It adopts the idea of observer mode. The generator of the event generates the event and sends it to the bound receiver. The receiving order is the same as the sending order

Dependency:

Easy to use:

Let's illustrate what happened:

The upstream sends data to the downstream, and the upstream and downstream generate a relationship through subscribe, that is, a subscription is reached

Resolution 1:

Observableemitter, what is this? Emitter: as the name suggests, it is the rxjava emitter. Through this emitter, you can send events - send different events by calling onnext, onerror and oncomplete methods

be careful:

Although rxjava can send events, it does not mean that you can send them at will, which needs to follow some rules

Onnext: you can send countless onnext, and each onnext recipient will receive it

Onerror: after sending the onerror event, the sender will continue to send the event after onerror, but the receiver will stop receiving the event after receiving the onerror

Oncomplete: after the oncomplete event is sent, the event after the sender's oncomplete will continue to be sent, but the receiver will stop receiving the event after receiving the oncomplete

The onerror event and oncomplete event are mutually exclusive, but this does not mean that if you configure multiple onerrors and oncompletes, they will crash. Multiple oncompletes can operate normally, but only the first one will be received, and the next one will not be received. When multiple onerrors are received, only the first one will be received, and the second one will directly cause the program to crash

Resolution 2:

What is disposable? After translation, baidu told me that it is called disposable. It is used to control the link between the sender and the receiver. The default is false, which means that the direct communication valve between the sender and the receiver is closed and can communicate normally. After calling the dispose() method, the valve is opened, which will block the communication between the sender and the receiver, thus disconnecting the connection

Overload method:

Resolution 3:

By default, both the sender and receiver run on the main thread, but this obviously does not meet the actual needs. In our daily use, we usually use the most time-consuming operations in the sub thread and then send them to the main thread. Is there no way for us to continue to use this excellent library? If you think too much of an excellent library, how can it be called excellent if you can't even think of it? Rxjava has a thread scheduler. Through the thread scheduler, we can easily achieve this effect. Here's the code

Final result:

You can see that we only added two lines of code to achieve the effect. Is there anything better and simpler than this?

matters needing attention:

Subscribeon() takes effect only when it is called for the first time. No matter how many times it is called, the first time will prevail

Observeon() can be called multiple times, and each call will change the thread

Several thread options in rxjava thread pool

This article is simple to use. The above is all the content of this article. I hope it will be helpful for everyone's learning. I also hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>