In the previous article, we learned about conversion operators that can transform data into the format we want, but what if there are some data in the data collection that we want to filter out? At this point we need to use the filter operator, which is somewhat similar to where in SQL, so that observable only returns data that satisfies our criteria.
The debounce operator is the limiting function that can be understood as a valve, when you half open the valve, the water will flow at a slower speed. The difference is that the water in the valve is not wasted, and the data filtered out by the debounce is discarded. In Rxjava, this operator is the atmosphere of the
Debounce of two operators. Take a look at the Throttlewithtimeout bar, as shown, this operator through time to limit the flow, the source observable each time a data is emitted will be timed, if the set of time before the end of the source observable new data emitted, This data is discarded and the timings are restarted. If the data is emitted every time before the end of the timer, the current limit will go to extremes: only the last data is emitted.
First we create a observable that emits a data every 100 milliseconds, and when the data to be emitted is a multiple of 3, the next data is delayed to 300 milliseconds before being launched.
- private Observable Createobserver () {
- return observable.create (new Observable. Onsubscribe () {
- @Override
- public void Call (subscriber !--? Super integer--> Subscriber) {
- for (int i = 0; i < i++) {
- if (!subscriber.isunsubscribed ()) {
- subsc Riber.onnext (i);
- }
- int sleep = +;
- if (i 3 = = 0) {
- sleep = +;
- }
- try {
- thread.sleep (sleep);
- } catch (Interruptedexception e) {
- e.printstacktrace ();
- }
-
- }
- subscriber.oncompleted ();
- }
- }). Subscribeon (Schedulers.computation ());
- }
Below use Throttlewithtimeout to filter this observable, we set the filter time is 200 milliseconds, also say that the launch interval is less than 200 milliseconds of data will be filtered out.
- Private Observable throttlewithtimeoutobserver () {
- Return Createobserver (). Throttlewithtimeout (Timeunit.milliseconds)
- . Observeon (Androidschedulers.mainthread ());
- }
Subscribe to it
- Mlbutton.settext ("Throttlewithtimeout");
- Mlbutton.setonclicklistener (E-throttlewithtimeoutobserver (). Subscribe (I--Log ("Throttlewithtimeout:" + i)) );
The result of the operation is as follows, it can be seen that the data not in multiples of 3 will emit new data within 200 milliseconds after launch, so it will be filtered out.
The debounce operator can also use time to filter, which is the same as throttlewithtimeout, but the deounce operator can also limit the flow based on a function. The return value of this function is a temporary observable, if the source observable when launching a new data, the previous data generated by the function of the temporary observable is not finished, then the previous data will be filtered out.
Generates a observable and filters it using Debounce, only the OnCompleted method is called when the emitted data is an even number to indicate that the temporary observable has been terminated.
- Private Observable debounceobserver () {
- Return Observable.just (1, 2, 3, 4, 5, 6, 7, 8, 9). Debounce (Integer, {
- Log (integer);
- Return observable.create (new Observable.onsubscribe () {
- @Override
- public void Call (Subscriber subscriber) {
- if (integer% 2 = = 0 &&!subscriber.isunsubscribed ()) {
- Log ("complete:" + integer);
- Subscriber.onnext (integer);
- Subscriber.oncompleted ();
- }
- }
- });
- })
- . Observeon (Androidschedulers.mainthread ());
- }
Subscribe to it
- Mrbutton.setonclicklistener (E-debounceobserver (). Subscribe (I--Log ("Debounce:" + i));
The result of the operation is as follows, it can be seen that only those data that call the OnCompleted method will be emitted, and the others are filtered out.
Second, Distinct
The purpose of the distinct operator is to go heavy, very well understood. As shown, all duplicate data will be filtered out. There is also an operator distinctuntilchanged, which is used to filter out continuous repeating data.
Create two observable and filter them separately using the distinct and distinctutilchanged operators
- Private Observable distinctobserver () {
- Return Observable.just (1, 2, 3, 4, 5, 4, 3, 2, 1). distinct ();
- }
- Private Observable distinctuntilchangedobserver () {
- Return Observable.just (1, 2, 3, 3, 3, 1, 2, 3, 3). distinctuntilchanged ();
- }
To subscribe
- Mlbutton.settext ("distinct");
- Mlbutton.setonclicklistener (E-distinctobserver (). Subscribe (I--log ("distinct:" + i));
- Mrbutton.settext ("untilchanged");
- Mrbutton.setonclicklistener (E-distinctuntilchangedobserver (). Subscribe (I--Log ("untilchanged:" + i));
The result of the operation is as follows, you can see distinct filter out all the duplicated numbers, two distinctutilchanged only filter out the duplicate numbers
Third, ElementAt, Filter
Both operators are well understood, and ElementAt only returns data at the specified location, and filter returns only the data that satisfies the filter, as shown below
Create two observable objects and filter them using the ElementAt and filter operators, respectively
- Private Observable elementatobserver () {
- Return Observable.just (0, 1, 2, 3, 4, 5). ElementAt (2);
- }
- Private Observable filterobserver () {
- Return Observable.just (0, 1, 2, 3, 4, 5). Filter (I-i < 3);
- }
Subscribe to it separately
- Mlbutton.settext ("ElementAt");
- Mlbutton.setonclicklistener (E-elementatobserver (). Subscribe (I--Log ("ElementAt:" + i));
- Mrbutton.settext ("Filter");
- Mrbutton.setonclicklistener (E-filterobserver (). Subscribe ("Filter:" + i));
The operation results are as follows
Iv. First and last
The first operator returns only the data, and the first data that satisfies the condition can also be returned. If you read my previous blog, you will find that in our example of using Rxjava to implement level three caching, we use the first operator to select the cache you want to use. In contrast to first, the last operator returns only the data that satisfies the condition.
There is also a blockingobservable method, this method does not do any processing to observable, will only block, and when the data that satisfies the condition is emitted, it will return a Blockingobservable object. can useObservable.toBlocking
OrBlockingObservable.from
method to convert a observable object into a Blockingobservable object. The blockingobservable can be used in conjunction with the first operator.
Create two observable objects and work with the first operator, respectively
- Private Observable Firstobserver () {
- Return Observable.just (0, 1, 2, 3, 4, 5). First (I-i > 1);
- }
- Private blockingobservable Filterobserver () {
- Return observable.create (New Observable.onsubscribe () {
- @Override
- public void Call (Subscriber subscriber) {
- for (int i = 0; i < 5; i++) {
- try {
- Thread.Sleep (500);
- } catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- if (!subscriber.isunsubscribed ()) {
- Log ("OnNext:" + i);
- Subscriber.onnext (i);
- }
- }
- if (!subscriber.isunsubscribed ()) {
- Subscriber.oncompleted ();
- }
- }
- }). toblocking ();
- }
To subscribe separately
- Mlbutton.settext ("first");
- Mlbutton.setonclicklistener (E-firstobserver (). Subscribe (I--log ("First:" + i));
- Mrbutton.settext ("Blocking");
- Mrbutton.setonclicklistener (E, {
- Log ("Blocking:" + filterobserver (). First (I-i > 1));
- });
The results of the operation are as follows. You can see that the first operator returns the number 2 that is greater than 1, while blockingobservable blocks until the first data that is greater than 1 is emitted.
V. Skip, take
The Skip operator filters out the first n items emitted by the source observable, while the take operator takes only the first n, which is easy to understand and use, but is useful. There are also skiplast and takelast operators, which are filtered from behind.
Create two observable and filter them using the skip and take operators, respectively
- Private Observable skipobserver () {
- Return Observable.just (0, 1, 2, 3, 4, 5). Skip (2);
- }
- Private Observable takeobserver () {
- Return Observable.just (0, 1, 2, 3, 4, 5). Take (2);
- }
To subscribe separately
- Mlbutton.settext ("Skip");
- Mlbutton.setonclicklistener (E-skipobserver (). Subscribe (I--Log ("Skip:" + i));
- Mrbutton.settext ("take");
- Mrbutton.setonclicklistener (E-takeobserver (). Subscribe (I--Log ("Take:" + i));
The result is as follows, you can see that skip filters out the first two items, while take filters out all other items except the first two.
Six, Sample, Throttlefirst
The sample operator periodically emits data from the source observable, and the others are filtered out, equivalent to the throttlelast operator, The Throttlefirst operator periodically launches the first data emitted by the source observable during this time period.
We create a observable that emits one data every 200 milliseconds and then filters it using the sample and Throttlefirst operators, respectively.
- Private Observable Sampleobserver () {
- Return Createobserver (). sample (Timeunit.milliseconds);
- }
- Private Observable Throttlefirstobserver () {
- Return Createobserver (). Throttlefirst (Timeunit.milliseconds);
- }
- Private Observable Createobserver () {
- Return observable.create (New Observable.onsubscribe () {
- @Override
- public void Call (Subscriber subscriber) {
- for (int i = 0; i <; i++) {
- try {
- Thread.Sleep (200);
- } catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- Subscriber.onnext (i);
- }
- Subscriber.oncompleted ();
- }
- });
- }
To subscribe separately
- Mlbutton.settext ("sample");
- Mlbutton.setonclicklistener (E-sampleobserver (). Subscribe (I--Log ("Sample:" + i));
- Mrbutton.settext ("Throttlefirst");
- Mrbutton.setonclicklistener (E-throttlefirstobserver (). Subscribe (I--Log ("Throttlefirst:" + i));
The result is as follows, you can see that the sample operator emits one data per 5 numbers, and Throttlefirst emits the first data every 5 data.
This demo program is shown in Github:https://github.com/chaoba/rxjavademo
http://www.bkjia.com/PHPjc/1077810.html www.bkjia.com true http://www.bkjia.com/PHPjc/1077810.html techarticle Rxjava operator (c) Filtering in the previous article, we learned about conversion operators that can convert data into the format we want, but if there are some of the data sets we want to ...