"Knowledge finishing" This may be the best Rxjava 2.x Getting Started Tutorial (iii)

Source: Internet
Author: User

Directory

    • First, preface
    • Second, the subject
      • 7, distinct
      • 8. Filter
      • 9. Buffer
      • 10. Timer
      • 11, Interval
      • 12, Doonnext
      • 13. Skip
      • 14. Take
      • 15, just
    • Third, written in the last

Body

This may be the best Rxjava 2.x introductory Tutorial Series Column Link: This might be the best Rxjava 2.x Getting Started tutorial (a) This may be the best Rxjava 2.x Getting Started Tutorial (ii) This may be the best Rxjava 2.x Getting Started Tutorial (iii) GITHUB Code Synchronization Update: Https://github.com/nanchen2251/RxJava2Examples

In order to satisfy everyone's hunger and thirst, GitHub will update the code, mainly including the Basic code encapsulation, RxJava 2.x all operator application scenarios and practical applications, in addition to RxJava may add other things later, in short, GitHub on the demo for everyone to build. Portal: Https://github.com/nanchen2251/RxJava2Examples

Back to top one, preface

Young old drivers, I am so diligent for everyone to share, but few urge more, OK. Actually writing this series is not for the eye, let's continue to write our RxJava 2.x operator.

Back to top two, the subject

7, distinct

This operator is very simple, popular, easy to understand, is simply to go to the heavy, I do not even want to paste code, but people, must persevere.

1 Observable.just (1, 1, 1, 2, 2, 3, 4, 5) 2                 . Distinct () 3                 . Subscribe (New consumer<integer> () {4                     @ OVERRIDE5 public                     Void Accept (@NonNull integer integer) throws Exception {6                         mrxoperatorstext.append ("distinct: "+ integer +" \ n "); 7                         log.e (TAG," distinct: "+ integer +" \ n "); 8                     }9                 });

Output:

The log log is obvious, and we have only 1,2,3,4,5 the events received by the receiver after Dinstinct ().

8. Filter

Believe me, filter you will be very common, its role is very simple, filter. Can accept a parameter to filter out values that do not meet our criteria

1 observable.just (1,, -5, 7, +) 2                 . Filter (new Predicate<integer> () {3                     @Override 4 public                     Boolea n Test (@NonNull integer integer) throws Exception {5                         return integer >=, 6                     } 7                 }). Subscribe (New Consumer <Integer> () {8             @Override 9 public             void Accept (@NonNull integer integer) throws Exception {Ten                 Mrxoperatorstext.append ("Filter:" + integer + "\ n"), one                 log.e (TAG, "filter:" + integer + "\ n");             }13         }) ;

Output:

As you can see, our filter has a value less than 10, so the best output is only 20,65,19.

9. Buffer

The buffer operator accepts two parameters, BUFFEF (Count,skip), to divide the data in Observable by Skip (step) into a maximum buffer that does not exceed count, and then generates a Observable. Perhaps you don't quite understand, we can further deepen it through our sample graphs and sample code.

1 Observable.just (1, 2, 3, 4, 5) 2                 . Buffer (3, 2) 3                 . Subscribe (New consumer<list<integer>> () {4                     @ Override 5 public                     void Accept (@NonNull list<integer> integers) throws Exception {6                         Mrxoperatorstext.append ("Buffer size:" + integers.size () + "\ n"); 7                         log.e (TAG, "Buffer size:" + integers.size () + "\ n"); 8                         mrxoperatorstext.append ("Buffer value:"); 9                         log.e (TAG, "Buffer value:"); Ten for                         (Integer i:integers) {One                             mrxoperatorstext.append (i + "");                             . E (Tag, i + ""),                         }14                         mrxoperatorstext.append ("\ n"),                         LOG.E (tag, "\ n");                     }17                 });

Output:

, we put the 1,2,3,4,5 in sequence, passing the buffer operator, where the parameter skip is 3, Count is 2, and our output is 123,345, 5 in turn. It is obvious that the first parameter of our buffer is count, which represents the maximum value, when the event is sufficient, it is generally the count value, and then skips the skip event every day. In fact, look at the log logs, I believe we all understand.

10. Timer

The timer is very interesting and is equivalent to a timed task. In 1.x it can also perform interval logic, but in 2.x this function is given to interval, which is described in the next section. However, it is important to note that both the timer and the interval are default on the new thread.

1 mrxoperatorstext.append ("Timer start:" + timeutil.getnowstrtime () + "\ n"); 2         log.e (TAG, "timer start:" + timeutil.getnowstrtime () + "\ n"); 3         Observable.timer (2, Timeunit.seconds) 4                 . Subscribeon (Schedulers.io ()) 5                 . Observeon ( Androidschedulers.mainthread ())//timer default on new thread, so need to switch back to main thread 6                 . Subscribe (New consumer<long> () {7                     @ Override 8 public                     void Accept (@NonNull Long along) throws Exception {9                         mrxoperatorstext.append ("Timer:" + along + "at" + timeutil.getnowstrtime () + "\ n"),                         log.e (TAG, "timer:" + along + "at" + timeutil.getnowstrtime () + "\ n" );                     }12                 });

Output:

Obviously, when we click the button two times to trigger the event, the reception is delayed for 2 seconds.

11, Interval

As we can say above, the interval operator is used for interval time to perform an operation, which accepts three parameters, namely, the first send delay, interval time, time unit.

1 mrxoperatorstext.append ("Interval start:" + timeutil.getnowstrtime () + "\ n"); 2         log.e (TAG, "interval start:" + timeutil.getnowstrtime () + "\ n"); 3         observable.interval (3,2, Timeunit.seconds) 4                 . Subscribeon (Schedulers.io ()) 5                 . Observeon ( Androidschedulers.mainthread ())//due to interval default in the new thread, we should cut back to the main thread 6                 . Subscribe (New consumer<long> () {7                     @Override 8 public                     void Accept (@NonNull Long along) throws Exception {9                         mrxoperatorstext.append ("interval:" + AL  Ong + "at" + timeutil.getnowstrtime () + "\ n"),                         log.e (TAG, "interval:" + along + "at" + timeutil.getnowstrtime () + "\ n");                     }12                 });

Output:

Like log logs, the first delay was received after 3 seconds, followed by 2 seconds each.

However, cautious's little partner may find that because we do this at intervals, when our activity is destroyed, the operation is actually still in progress, so we have to take a little thought to get rid of it when we don't need it. Looking at the source code discovery, we subscribe (COUSUMER<? Super t> OnNext) returned disposable, and we can make a fuss over it.

 1 @Override 2 protected void DoSomething () {3 Mrxoperatorstext.append ("Interval start:" + timeutil.getnows Trtime () + "\ n"); 4 LOG.E (TAG, "interval start:" + timeutil.getnowstrtime () + "\ n");                 5 mdisposable = Observable.interval (3, 2, Timeunit.seconds) 6. Subscribeon (Schedulers.io ()) 7 . Observeon (Androidschedulers.mainthread ())//due to interval default on new threads, we should cut back to the main thread 8. Subscribe (New Co Nsumer<long> () {9 @Override10 public void Accept (@NonNull Long along) throw  S Exception {mrxoperatorstext.append ("interval:" + along + "at" + timeutil.getnowstrtime () +                     "\ n"); LOG.E (TAG, "interval:" + along + "at" + timeutil.getnowstrtime () + "\ n"); 13 }14});}16 @Override18 protected void OnDestroy () {Super.ondestroy (); if (mdisposable! = null &&!mDisposable.isdisposed ()) {mdisposable.dispose (); 22}23} 

Haha, once more verified, solved our doubts.

12, Doonnext

Actually think that Doonnext should not count as an operator, but considering its common use, we still bite teeth to put it here. Its role is to allow subscribers to do something interesting before they receive the data. If we want to save it before we get the data, we can certainly do that.

1 Observable.just (1, 2, 3, 4) 2                 . Doonnext (New Consumer<integer> () {3                     @Override 4 public                     void Accept (@N Onnull integer integer) throws Exception {5                         mrxoperatorstext.append ("Doonnext save" + Integer + "success" + "\ n"); 6                         log.e (TAG, "Doonnext save" + integer + "success" + "\ n"); 7                     } 8                 }). Subscribe (New consumer<integer> () {9             @Override10 public             void Accept (@NonNull Integer Integer) throws Exception {one                 mrxoperatorstext.append ("Doonnext:" + integer + "\ n"),                 log.e (TAG, "Doonnext:" + integer + "\ n");             }14         });

Output:

13. Skip

Skip is very interesting, in fact, the function is the same as the literal meaning, accept a long parameter count, which means to skip count number to start receiving.

1  observable.just (1,2,3,4,5) 2                 . Skip (2) 3                 . Subscribe (New consumer<integer> () {4                     @Override5 Public                     void Accept (@NonNull integer integer) throws Exception {6                         mrxoperatorstext.append ("Skip:" +integer + "\ n "); 7                         log.e (TAG," Skip: "+integer +" \ n "); 8                     }9                 });

Output:

14. Take

Take, accept a long parameter count, which represents the maximum count of data received.

1  flowable.fromarray (1,2,3,4,5) 2                 . Take (2) 3                 . Subscribe (New consumer<integer> () {4                     @ OVERRIDE5 public                     Void Accept (@NonNull integer integer) throws Exception {6                         mrxoperatorstext.append ("Take:" + Integer + "\ n"), 7                         log.e (TAG, "Accept:take:" +integer + "\ n"); 8                     }9                 });

Output:

15, just

Just, there is nothing to say, in fact, in the previous examples are explained, is a simple launcher in turn called the OnNext () method.

1 Observable.just ("1", "2", "3") 2                 . Subscribeon (Schedulers.io ()) 3                 . Observeon (Androidschedulers.mainthread ( ) 4                 . Subscribe (New consumer<string> () {5                     @Override 6 public                     void Accept (@NonNull String s) throws Exc eption {7                         mrxoperatorstext.append ("Accept:onnext:" + S + "\ n"); 8                         log.e (TAG, "Accept:onnext:" + S + "\ n"); 9                     }10                 });

Output:

Back to the top three, written at the end of the

Well, in this section first here, the next section we continue to talk about simple operators, although our tutorial is more boring, and now not so much attention, but the series I believe that we will be very much like, we see you next time!

"Knowledge finishing" This may be the best Rxjava 2.x Getting Started Tutorial (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.