Rxjava2. X implementation timer example code

preface

Since the network layer has been upgraded to rxjava 2. X, some adjustments need to be made. Although rxjava1. X and rxjava2. X belong to rxjava series, rxjava2. X and rxjava1. X are two different versions due to the rewriting of some codes of rxjava2. X. rxjava2. X has better performance, especially in back pressure support. Of course, the focus of this article is not on the differences in the RX version. Interested students can study it by themselves. Of course, one of the differences between 2. X and 1. X is that there is no subscription msubscription in 2. X, and methods such as observable. Create () no longer return subscription objects. Instead, an onsubscribe (@ nonnull dispose dispose) method will be returned in the new observer() method, and dispose dispose dispose is the object we need to unsubscribe, The methods include:

Dispose(): unsubscribe

Isdisposed(): judge whether the subscription has been cancelled

OK, with these foreshadowing, let's get to the point.

The first step is to import the rxjava2. X dependency package

Step 2: directly add the rxtimerutil code of the timer class

It should be noted that if you use delayed execution once, you do not need to cancel after calling timer, because I have handled it in the code. If it is a method called for repeated execution, you need to cancel the subscription in the ondestroy method of activity, as shown below:

How simple and rough, well, let's stop here today. I hope it will be helpful to everyone's learning, and I 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
分享
二维码
< <上一篇
下一篇>>