Rxjava debounce operator

Source: Internet
Author: User
Tags throwable

Debounce1. Official definition only emit an item from an Observable if a particular timespan have passed without it emitting another item

The debounce operator filters out items emitted by the source Observable that is rapidly followed by another emitted Item.

2.API
 Public Final Observable<t> debounce (long timeout, timeunit unit);                       // Default Execution Thread schedulers.computation ()  Public Final Observable<t> debounce (long timeout, timeunit unit, Scheduler Scheduler);
Using scenes in 3.Android

Click the button quickly to perform an action.

For example, the selection package in the American group app: the 0-part Quick Click on the left image to 7 copies of the right, then the total price based on the number of selected copies.

4. The code implements a
// Numberpickerview.java  -> {      selectcount++      ; + "");      Onchangelistener.onchange (Dealid, selectcount);    // Dealid is the ID of the current package });  Public Interface Onchangelistener {      onChange (intint  selectcount);}

// Activity   {      calculatedealprice (dealid, SelectCount);}); private calculatedealprice (intint  selectcount) {     //  calculated price }

For this quick click, we actually need to calculate the 7th time, the middle of a series of transient state is not necessary to calculate, using debounce to solve.

5. Code implementation Two: Increase debounce operation
rxview.clicks (plusview)              {                 SelectCount+ +;                  + "");                  return selectcount;             }             . Debounce (timeunit.milliseconds))             . Observeon (Androidschedulers.mainthread (             ))- > Onchangelistener.onchange (Dealid, SelectCount), Throwable::p rintstacktrace);

Disadvantages:

1.NumberPickerView relies on the com.jakewharton.rxbinding:rxbinding:x.x.x

2.NumberPickerView Plusview is forced to increase the debounce operation of 400ms

5. Code implementation Three: Move the debounce operation out of the Numberpickerview
// Numberpickerview.java  -> {      selectcount++      ; + "");      Onchangelistener.onchange (Dealid, selectcount);    // Dealid is the ID of the current package});

//Activity... Publishsubject<SelectParams> subject =publishsubject.create (); Numberpickerview.setonchangelistener (Dealid, SelectCount)-{Subject.onnext (Newselectparams (Dealid, SelectCount);}); Subject.debounce (400, Timeunit.milliseconds). Observeon (Androidschedulers.mainthread ()). Subscribe (Selectparams-Calculatedealprice (Selectparams.dealid, Selectparams.selectcount), Throwable::p rintstacktrace);classSelectparams {intDealid; intSelectCount; Selectparams (intDealid,intSelectcont) {       This. Dealid =Dealid;  This. SelectCount =SelectCount; }}PrivateCalculatedealprice (intDealid,intSelectCount) {     ... //Calculate Price}

At this point Numberpickerview no longer relies on third-party libraries for improved applicability.

Reference: http://reactivex.io/documentation/operators.html

Rxjava debounce operator

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.