RxJava2 / RxAndroid2操作符skip

时间:2022-02-10 17:47:58

RxJava2 / RxAndroid2操作符skip


顾名思义,skip跳过,例如:

package zhangphil.app;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private final String TAG = getClass().getSimpleName();
    private CompositeDisposable mCompositeDisposable = null;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCompositeDisposable = new CompositeDisposable();

        test();
    }

    private void test() {
        DisposableObserver disposableObserver = new DisposableObserver<Integer>() {
            @Override
            public void onNext(Integer i) {
                Log.d(TAG, "#####开始#####");
                Log.d(TAG + "数据", String.valueOf(i));
                Log.d(TAG, "#####结束#####");
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "onComplete");
            }

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

        mCompositeDisposable.add(getObservable()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .skip(5)
                .subscribeWith(disposableObserver));
    }

    private Observable<Integer> getObservable() {
        return Observable.range(0, 10);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // 如果退出程序,就清除后台任务
        mCompositeDisposable.clear();
    }
}



代码运行结果:

05-16 14:22:09.606 16806-16806/zhangphil.app D/MainActivity: #####开始#####
05-16 14:22:09.606 16806-16806/zhangphil.app D/MainActivity数据: 5
05-16 14:22:09.606 16806-16806/zhangphil.app D/MainActivity: #####结束#####
05-16 14:22:09.606 16806-16806/zhangphil.app D/MainActivity: #####开始#####
05-16 14:22:09.606 16806-16806/zhangphil.app D/MainActivity数据: 6
05-16 14:22:09.606 16806-16806/zhangphil.app D/MainActivity: #####结束#####
05-16 14:22:09.606 16806-16806/zhangphil.app D/MainActivity: #####开始#####
05-16 14:22:09.606 16806-16806/zhangphil.app D/MainActivity数据: 7
05-16 14:22:09.606 16806-16806/zhangphil.app D/MainActivity: #####结束#####
05-16 14:22:09.606 16806-16806/zhangphil.app D/MainActivity: #####开始#####
05-16 14:22:09.606 16806-16806/zhangphil.app D/MainActivity数据: 8
05-16 14:22:09.606 16806-16806/zhangphil.app D/MainActivity: #####结束#####
05-16 14:22:09.606 16806-16806/zhangphil.app D/MainActivity: #####开始#####
05-16 14:22:09.606 16806-16806/zhangphil.app D/MainActivity数据: 9
05-16 14:22:09.606 16806-16806/zhangphil.app D/MainActivity: #####结束#####
05-16 14:22:09.606 16806-16806/zhangphil.app D/MainActivity: onComplete