RxJava Operator Series 3 (Part 2)

RxJava Operator Series 3 (Part 2)

[[180593]]

Continued from previous article

Take

The Take operator modifies the behavior of an Observable to return only the first N items of data, then emit a completion notification and ignore the remaining data.

  1. Observable.range(1,8)
  2.  
  3. .take(4)
  4.  
  5. .subscribe(new Subscriber <Integer> () {
  6.  
  7. @Override
  8.  
  9. public void onNext( Integer item) {
  10.  
  11. Log.e(TAG, "Next: " + item);
  12.  
  13. }
  14.  
  15.   
  16.  
  17. @Override
  18.  
  19. public void onError(Throwable error) {
  20.  
  21. Log.e(TAG, "Error: " + error.getMessage());
  22.  
  23. }
  24.  
  25.   
  26.  
  27. @Override
  28.  
  29. public void onCompleted() {
  30.  
  31. Log.e(TAG, "complete." );
  32.  
  33. }
  34.  
  35. });

Output log information

  1. Next : 1
  2.  
  3. Next : 2
  4.  
  5. Next : 3
  6.  
  7. Next : 4
  8.  
  9. complete

Like skip, take also has two other overloaded methods: take(long time, TimeUnit unit) and take(long time, TimeUnit unit, Scheduler scheduler), which are executed on the computation scheduler by default.

take also has a variant operator TakeLast, takeLastBuffer. The specific execution effect can be coded by yourself.

Debounce

This operator means that a data is emitted only when no data is emitted after a specified period of time. It may sound a bit confusing. You can understand it as filtering the results generated by the source Observable interval. If no other results are generated within this specified interval, the result is submitted to the subscriber, otherwise the result is ignored. The principle is a bit like optical image stabilization.

On the code

  1. Observable.range(1,8)
  2.  
  3. .take(4)
  4.  
  5. .subscribe(new Subscriber <Integer> () {
  6.  
  7. @Override
  8.  
  9. public void onNext( Integer item) {
  10.  
  11. Log.e(TAG, "Next: " + item);
  12.  
  13. }
  14.  
  15.   
  16.  
  17. @Override
  18.  
  19. public void onError(Throwable error) {
  20.  
  21. Log.e(TAG, "Error: " + error.getMessage());
  22.  
  23. }
  24.  
  25.   
  26.  
  27. @Override
  28.  
  29. public void onCompleted() {
  30.  
  31. Log.e(TAG, "complete." );
  32.  
  33. }
  34.  
  35. });

Output information

  1. onNext: 4
  2.  
  3. onNext: 5
  4.  
  5. onNext: 6
  6.  
  7. onNext: 7
  8.  
  9. onNext: 8
  10.  
  11. onNext: 9
  12.  
  13. onCompleted:

The output data may not be the same, it may start from 5.

Distinct

This is relatively easy to understand. It filters out duplicate data and only allows data items that have not yet been emitted to pass.

Sample Code

  1. Observable.just(0, 0, 6, 4, 2, 8, 2, 1, 9, 0)
  2.  
  3. .distinct ()
  4.  
  5. .subscribe(new Subscriber <Integer> () {
  6.  
  7. @Override
  8.  
  9. public void onCompleted() {
  10.  
  11. Log.e(TAG, "onCompleted:Distinct " );
  12.  
  13. }
  14.  
  15.   
  16.  
  17. @Override
  18.  
  19. public void onError(Throwable e) {
  20.  
  21. Log.e(TAG, "onError:Distinct " );
  22.  
  23. }
  24.  
  25.   
  26.  
  27. @Override
  28.  
  29. public void onNext( Integer   integer ) {
  30.  
  31. Log.e(TAG, "onNext:Distinct " + integer );
  32.  
  33. }
  34.  
  35. });

Output log information

  1. onNext: Distinct 0
  2.  
  3. onNext: Distinct 6
  4.  
  5. onNext: Distinct 4
  6.  
  7. onNext: Distinct 2
  8.  
  9. onNext: Distinct 8
  10.  
  11. onNext: Distinct 1
  12.  
  13. onNext: Distinct 9
  14.  
  15. onCompleted: Distinct  

ElementAt

This operator gets the data item at the specified index position of the data sequence emitted by the original Observable, and then emits it as its own first data. Pass it a 0-based index value, and it will emit the value of the corresponding index position of the original Observable data sequence. If the value you pass to elementAt is 4, it will emit the data of the 5th item. The following example code

  1. Observable.just(0, 0, 6, 4, 2, 8, 2, 1, 9, 0)
  2.  
  3. .elementAt(4)
  4.  
  5. .subscribe(new Subscriber <Integer> () {
  6.  
  7. @Override
  8.  
  9. public void onCompleted() {
  10.  
  11. Log.e(TAG, "onCompleted:ElementAt " );
  12.  
  13. }
  14.  
  15.   
  16.  
  17. @Override
  18.  
  19. public void onError(Throwable e) {
  20.  
  21. Log.e(TAG, "onError:ElementAt " );
  22.  
  23. }
  24.  
  25.   
  26.  
  27. @Override
  28.  
  29. public void onNext( Integer   integer ) {
  30.  
  31. Log.e(TAG, "onNext:ElementAt " + integer );
  32.  
  33. }
  34.  
  35. });

Output log information

  1. onNext:ElementAt 2
  2.  
  3. onCompleted:ElementAt

IgnoreElements

The operator suppresses all data emitted by the original Observable and only allows its termination notification (onError or onCompleted) to pass through. The onNext() method will not be executed using this operator.

  1. Observable.just(1, 2, 3).ignoreElements().subscribe(new Subscriber() {
  2.  
  3. @Override
  4.  
  5. public void onCompleted() {
  6.  
  7. Log.e(TAG, "onCompleted" );
  8.  
  9. }
  10.  
  11.  
  12.  
  13. @Override
  14.  
  15. public void onError(Throwable e) {
  16.  
  17. Log.e(TAG, "onError" );
  18.  
  19. }
  20.  
  21.  
  22.  
  23. @Override
  24.  
  25. public void onNext( Integer   integer ) {
  26.  
  27. Log.e(TAG, "onNext" );
  28.  
  29. }
  30.  
  31. });

After execution, only onCompleted will be output. The effect of this operator is just like the empty() method to create an empty Observable, and only the onCompleted() method will be executed. The difference is that ignoreElements processes the data source, while empty() creates an Observable.

<<:  RxJava Operator Series 3 (Part 1)

>>:  A collection of ViewHolder tool classes

Recommend

What makes Apple users so loyal? What can Chinese smartphones learn from?

In the field of smart phones, iPhone user loyalty...

Detailed explanation of Universal Studios’ 7 super marketing IPs!

The opening and popularity of Universal Studios B...

I'm awake, but I can't move my body? The reason is...

© Wellcome Collection Leviathan Press: Sleep para...

The evolution of smartphones: What will smartphones look like in the next year?

On December 10, Samsung and Huawei Honor both lau...

Sharing of advertising cases on WeChat Moments - wedding industry!

Brand Name: MuseMarry After one week of launch, c...

How to use data thinking to design? Take a look at this practical case!

When we enter the daily experience design process...

De Ge's Stock Discussion: Thirteen Short-term Hot Money Lessons

De Ge's Stock Discussion: Thirteen Short-term...

For a product with 100,000 users, how should user activation be carried out?

For user operations personnel, if the product has...