role

Determines whether an event sent by an Observable meets conditions by setting functions

type

type role note
all() Determine whether all data sent meets the function condition Returns Single, true if so
takeWhile() Start to meet the function conditions before launching data, until not meet the launch will stop No further launches will be made regardless of whether the conditions are met
takeUntil() Stop firing until the function is satisfied No further launches will be made regardless of whether the conditions are met
skipWhile() If the function condition is not met, the data is transmitted All the subsequent data is emitted
skipUntil() The first Observable doesn’t emit data until the second one does, and the previous ones are discarded The argument is Observable, not Predicate
SequenceEqual() Determine whether two Observables need to send the same data Return true if the same
contains() Checks whether the sent data contains the specified data Contains return true
isEmpty() Check whether the sent data is null Returns true if empty
amb() When multiple Observables need to be sent, only the data of the first one is sent, and the rest are discarded
defaultIfEmpty() Send the Next event normally; Or send a default without any valid Next events and only a Complete event

OfType () : emits only events of a specified type

Observable.just(1."2")
        .all(new Predicate<Serializable>() {
            @Override
            public boolean test(Serializable serializable) throws Exception {
                return serializable instanceof String;
            }
        })
        .subscribe(new SingleObserver<Boolean>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "Function condition onSubscribe");
            }

            @Override
            public void onSuccess(Boolean aBoolean) {
                Log.d(TAG, "Function condition:" + aBoolean);
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "Function condition onError"); }}); >>>>>> function condition onSubscribe function condition:false
>>>>>>
Copy the code

TakeWhile () : data will be transmitted when the function conditions are met at the beginning, and the data will be stopped until the function conditions are not met, and the data will not be transmitted no matter whether the conditions are met

Observable.just(1.2.3.1.2.3)
        .takeWhile(new Predicate<Integer>() {
            @Override
            public boolean test(Integer integer) throws Exception {
                return integer > 2;
            }
        })
        .subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                Log.d(TAG, "Data satisfying the function condition:"+ integer); }}); >>>>>> >>>>>>Copy the code

TakeUntil () : stop launching until the function conditions are met, and stop launching no matter whether the conditions are met

Observable.just(1.2.3.1.2.3)
        .takeUntil(new Predicate<Integer>() {
            @Override
            public boolean test(Integer integer) throws Exception {
                return integer > 2;
            }
        })
        .subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                Log.d(TAG, "Data satisfying the function condition:"+ integer); }}); >>>>>> Data satisfying the function condition:1Data satisfying the function condition:2Data satisfying the function condition:3
>>>>>>
Copy the code

SkipWhile () : discards transmitted data until the function condition is not met, and all subsequent data is transmitted

Observable.just(1.2.3.1.2.3)
        .skipWhile(new Predicate<Integer>() {
            @Override
            public boolean test(Integer integer) throws Exception {
                returninteger ! =3;
            }
        })
        .subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                Log.d(TAG, "Data satisfying the function condition:"+ integer); }}); >>>>>> Data satisfying the function condition:3Data satisfying the function condition:1Data satisfying the function condition:2Data satisfying the function condition:3
>>>>>>
Copy the code

SkipUntil () : The first Observable doesn’t emit data until the second one does, and the previous ones are discarded

Observable.just(1.2.3.1.2.3)
        .skipUntil(new ObservableSource<Object>() {
            @Override
            public void subscribe(Observer<? super Object> observer) {
                Observable.timer(1, TimeUnit.SECONDS).subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        Log.d(TAG, "Delay launch by one second."); }}); } }) .subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                Log.d(TAG, "Data satisfying the function condition:"+ integer); }}); >>>>>> One second delay >>>>>>Copy the code

SequenceEqual () : Determines whether two Observables need to send the same data

Observable.sequenceEqual(Observable.just(1), Observable.just(1))
        .subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                Log.d(TAG, "Whether the conditions are met:"+ aBoolean); }}); >>>>>> Whether the following conditions are met:true
>>>>>>
Copy the code

Contains () : determines whether the sent data contains the specified data

Observable.just(1.2.3.1.2.3)
        .contains(1)
        .subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                Log.d(TAG, "Whether the conditions are met:"+ aBoolean); }}); >>>>>> Whether the following conditions are met:true
>>>>>>
Copy the code

IsEmpty () : checks whether the data sent isEmpty

Observable.empty()
        .isEmpty()
        .subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                Log.d(TAG, "Whether the conditions are met:"+ aBoolean); }}); >>>>>> Whether the following conditions are met:true
>>>>>>
Copy the code

DefaultIfEmpty () : Emits a default value if the data sent is empty

Observable.empty()
        .defaultIfEmpty(99)
        .subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object object) throws Exception {
                Log.d(TAG, "Data satisfying the function condition:"+ object); }}); >>>>>> Data satisfying the function condition:99
>>>>>>
Copy the code