Android开发录Android-Rxjava&retrofit&daggerAndroid-RxJava

看完不懂Rxjava我跪搓板(2)

2018-04-26  本文已影响173人  付凯强

0. 为了月薪1.8万

奋斗方向和目标.png

1. 序言

上一篇博客讲述内容如下:

2. 过滤操作符

过滤操作符,顾名思义,就是对数据进行过滤,过滤掉我们不想要的,得到我们想要的。

Observable.just(1, 2, 3, 4).filter(new Func1<Integer, Boolean>() {
            @Override
            public Boolean call(Integer integer) {
                return integer > 2;
            }
        }).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                Log.d(TAG, "FILTER:" + integer);
            }
        });
04-26 21:32:40.319 11704-11704/? D/MainActivity: FILTER:3
04-26 21:32:40.319 11704-11704/? D/MainActivity: FILTER:4
Observable.just(1, 2, 3, 4).elementAt(2).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                Log.d(TAG, "FILTER:" + integer);
            }
        });
Observable.just(1, 2, 3, 4).elementAt(2).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                Log.d(TAG, "FILTER:" + integer);
            }
        });
 Observable.just(1, 2, 2, 3, 4).distinct().subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                Log.d(TAG, "FILTER:" + integer);
            }
        });
04-26 09:38:46.882 1786-1786/? D/MainActivity: FILTER:1
04-26 09:38:46.882 1786-1786/? D/MainActivity: FILTER:2
04-26 09:38:46.882 1786-1786/? D/MainActivity: FILTER:3
04-26 09:38:46.882 1786-1786/? D/MainActivity: FILTER:4
 Observable.just(1, 2, 3,2, 3, 4).distinctUntilChanged().subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                Log.d(TAG, "FILTER:" + integer);
            }
        });
04-26 09:41:14.589 1964-1964/? D/MainActivity: FILTER:1
04-26 09:41:14.589 1964-1964/? D/MainActivity: FILTER:2
04-26 09:41:14.589 1964-1964/? D/MainActivity: FILTER:3
04-26 09:41:14.589 1964-1964/? D/MainActivity: FILTER:2
04-26 09:41:14.589 1964-1964/? D/MainActivity: FILTER:3
04-26 09:41:14.589 1964-1964/? D/MainActivity: FILTER:4
 Observable.just(1, 2, 3,2, 3, 4).skip(2).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                Log.d(TAG, "FILTER:" + integer);
            }
        });
04-26 09:51:22.934 2073-2073/com.example.multidownload D/MainActivity: FILTER:3
04-26 09:51:22.935 2073-2073/com.example.multidownload D/MainActivity: FILTER:2
04-26 09:51:22.935 2073-2073/com.example.multidownload D/MainActivity: FILTER:3
04-26 09:51:22.935 2073-2073/com.example.multidownload D/MainActivity: FILTER:4
 Observable.just(1, 2, 3,2, 3, 4).take(2).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                Log.d(TAG, "FILTER:" + integer);
            }
        });
04-26 09:52:55.417 2157-2157/? D/MainActivity: FILTER:1
04-26 09:52:55.418 2157-2157/? D/MainActivity: FILTER:2
Observable.just(1, 2, 3,2, 3, 4).skipLast(2).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                Log.d(TAG, "FILTER:" + integer);
            }
        });
04-26 09:54:46.296 2242-2242/? D/MainActivity: FILTER:1
04-26 09:54:46.296 2242-2242/? D/MainActivity: FILTER:2
04-26 09:54:46.296 2242-2242/? D/MainActivity: FILTER:3
04-26 09:54:46.296 2242-2242/? D/MainActivity: FILTER:2
 Observable.just(1, 2, 3,2, 3, 4).takeLast(2).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                Log.d(TAG, "FILTER:" + integer);
            }
        });
04-26 09:54:46.296 2242-2242/? D/MainActivity: FILTER:3
04-26 09:54:46.296 2242-2242/? D/MainActivity: FILTER:4
Observable.just(1, 2, 3, 2, 3, 4).ignoreElements().subscribe(new Observer<Integer>() {
            @Override
            public void onCompleted() {
                Log.d(TAG, "onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "onError");
            }

            @Override
            public void onNext(Integer integer) {
                Log.d(TAG, "onNext");
            }
        });
04-26 10:09:33.294 2362-2362/com.example.multidownload D/MainActivity: onCompleted
Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                for (int i = 0; i < 10; i++) {
                    subscriber.onNext(i);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                subscriber.onCompleted();
            }
        }).throttleFirst(200, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.d(TAG, "INTEGER:" + integer);
                    }
                });
04-26 11:02:57.708 2784-2784/? D/MainActivity: INTEGER:0
04-26 11:02:57.930 2784-2784/? D/MainActivity: INTEGER:2
04-26 11:02:58.135 2784-2784/? D/MainActivity: INTEGER:4
04-26 11:02:58.336 2784-2784/? D/MainActivity: INTEGER:6
04-26 11:02:58.543 2784-2784/? D/MainActivity: INTEGER:8
  Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                for (int i = 0; i < 10; i++) {
                    subscriber.onNext(i);
                    int sleep = 100;
                    if (i % 3 == 0) {
                        sleep = 300;
                    }
                    try {
                        Thread.sleep(sleep);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                subscriber.onCompleted();
            }
        }).throttleWithTimeout(200, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.d(TAG, "INTEGER:" + integer);
                    }
                });
04-26 11:19:35.525 3408-3422/com.example.multidownload D/MainActivity: INTEGER:0
04-26 11:19:36.030 3408-3422/com.example.multidownload D/MainActivity: INTEGER:3
04-26 11:19:36.541 3408-3422/com.example.multidownload D/MainActivity: INTEGER:6
04-26 11:19:37.037 3408-3422/com.example.multidownload D/MainActivity: INTEGER:9

3. 后续

如果大家喜欢这篇文章,欢迎点赞;如果想看更多前端移动端后端Java或Python方面的技术,欢迎关注!

上一篇 下一篇

猜你喜欢

热点阅读