Some “pits” stepped in the process of upgrading rxjava 1 to rxjava 2

Rxjava2 introduction

Rxjava2 has been released for some time. It is a major upgrade to rxjava. One of my libraries cv4j uses rxjava2 for a taste, but rxjava2 and rxjava1 cannot exist in the same project at the same time. I have to upgrade to the latest version where rxjava is used in all my frameworks and apps. So I sorted out and recorded some filled pits. Share it for your reference and study. Let's see the detailed introduction below:

Pit filling record

1. Rxjava1 and rxjava2 cannot coexist

If rxjava1 and rxjava2 are used simultaneously in the same module, it is similar to the following:

Well, unfortunately you will encounter such a mistake

Similarly, if rxjava2 is used in the app, but a third-party library still uses rxjava1, the same error will be encountered.

The above error is because rxandroid 2.0.1 itself relies on rxjava 2.0.1. We tried to get rid of the dependence on rxjava, leaving only rxandroid. There will still be problems.

Therefore, using rxandroid cannot remove the dependence on rxjava. I use it like this.

The official explanation is the same

Finally, I suggest that when upgrading to rxjava2, you must upgrade everything you use and use the latest version.

2. Add flowable

Observable in rxjava1 does not support backpressure well, and missingbackpressureexception will be thrown. Therefore, in rxjava2, oberservible no longer supports backpressure, but uses the new flowable to support backpressure.

The usage of flowable is the same as that of the original observable.

3. Name change of actionn and funcn

Actionn and funcn follow the naming rules of Java 8. Among them, action0 is renamed action, action1 is renamed consumer, action2 is renamed biconsumer, action3 - action9 are no longer used, and actionn becomes consumer < object [] >.

Similarly, func is renamed function, func2 is renamed bifunction, func3 - func9 is renamed function3 - function9, and funcn is replaced by function < object [], R >.

4. Observable.onsubscribe becomes observableonsubscribe

The original writing of rxjava1:

Now write:

5. Observableemitter is used in observableonsubscribe to send data to observer

Combined with the previous article, observableonsubscribe no longer uses subscriber, but uses observableemitter instead.

Observableemitter can be understood as an emitter and is used to send events. It can send three types of events. It can send next events, complete events and error events respectively by calling onnext (t value), oncomplete() and onerror (throwable error) of emitter. If you only care about the next event, just use onnext () alone.

It should be noted that after the emitter's oncomplete() call, the consumer will no longer receive any next events.

6. Observable. Transformer becomes observable transformer

The original writing of rxjava1:

Now write:

As the flowable is added, the flowabletransformer is also added

7. The subscription is renamed disposable

In rxjava2, because the org.reactivestreams.subscription class already exists, in order to avoid name conflict, the original rx.subscription is renamed io.reactivex.disposables.disposable.

I didn't know at the beginning. When upgrading rxjava2, I found that org.reactivestreams.subscription can't do the original rx.subscription at all:(

By the way, disposable must be used once and destroyed when used up.

8. Change of first() usage

The official documentation describes the use of first ()

Take first (func1) as an example. Push () is used after first (func1), which was originally written by rxjava1

Rxjava2 changed to this

9. Toblocking(). Y is replaced by blockingy()

There is an optional class in my framework, which is similar to the optional function of Java 8. It was originally written using rxjava1.

After upgrading to rxjava2, the get () and orelse () methods will report errors. After modification, this is the case.

10. PublishSubject

Backpressure is no longer supported for publishsubject and various subjects (replaysubject, behaviorsubject, asyncsubject).

summary

Rxjava2 has brought far more changes than these. I will continue to sort out and summarize if I encounter it in the future. After all, rxjava2 I use is still a small part of the content.

Rxjava 2 is still an official document. If it is a new project, you can use rxjava2 without hesitation. If it is a mature and stable project online, you can wait. For novices, you can learn directly from rxjava2 and skip rxjava1. For veterans, rxjava2 still uses the original idea, with little difference. It doesn't take much time to migrate from rxjava1 to rxjava2.

Well, the above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for 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
分享
二维码
< <上一篇
下一篇>>