In this article, we will look at several operators that determine conditions related to Boolean operators.

The All operator.

Determines whether all data emitted by an Observable meets a condition. That is, determine whether the data sent by a data source conforms to the rule and output a Boolean return value. This effect is the same as filter. The all operator forces the output type to be a Boolean, so use all or filter depending on the scenario.

  • For example, in a list of questions, clicking on an item jumps to a chat room that automatically sends multiple question messages. (Kind of like jingdong, Tmall find customer service, automatically send some certain questions)
Public Observable<Boolean> autoSendMsgList(ArrayList<AutoSendMsgModel> autoSendMsgList) {return Observable.fromIterable(autoSendMsgList)
                .flatMap(new Function<AutoSendMsgModel, ObservableSource<Boolean>>() {
                    @Override
                    public ObservableSource<Boolean> apply(AutoSendMsgModel model) throws Exception {
                        return sendTextMsg(model.getMsg());
                    }
                })
                .all(new Predicate<Boolean>() {
                    @Override
                    public boolean test(Boolean isSuccess) throws Exception {
                        return isSuccess;
                    }
                }).toObservable();
    }
Copy the code
  • The all operator determines that each sent message must return true to be successful.

Amb operator.

Given two or more Observables, it emits only all data from the Observable that first emits data or notifications, and the rest is discarded. It means competition, whoever sends the event first wins. Either the onNext(), onError or onCompleted() event.

  • This scenario is easy to understand. For example, before we request an interface, we first go to the in-memory cache, then we go to the local cache, and finally when there is no cache available, we request the interface data, and if there is a cache, the interface does not request it.
Observable.ambArray(
                                Observable.create(new ObservableOnSubscribe<List<SearchTagModel>>() {
                                    @Override
                                    public void subscribe(ObservableEmitter<List<SearchTagModel>> emitter) throws Exception {
                                        if (mHotTagCacheList.size() > 0) {
                                            emitter.onNext(mHotTagCacheList);
                                            emitter.onComplete();
                                        }
                                    }
                                })
                                        .map(new Function<List<SearchTagModel>, BaseSearchModel>() {
                                            @Override
                                            public BaseSearchModel apply(List<SearchTagModel> tagModels) throws Exception {
                                                mHotTagList.clear();
                                                mHotTagList.addAll(tagModels);
                                                returnnew HotSearchModel(tagModels); }}), / / hot search mClient requestTagsInObservable (TAG). The filter (new Predicate < HttpModel < List < SearchTagModel > > > () {@ Override public booleantest(HttpModel<List<SearchTagModel>> httpModel) throws Exception {
                                                returnhttpModel.getData() ! = null && httpModel.getData().size() > 0; } }) .map(new Function<HttpModel<List<SearchTagModel>>, BaseSearchModel>() { @Override public BaseSearchModel apply(HttpModel<List<SearchTagModel>> httpModel) throws Exception {// Cache mhottagcachelist.clear (); mHotTagCacheList.addAll(httpModel.getData()); mHotTagList.clear(); mHotTagList.addAll(httpModel.getData());returnnew HotSearchModel(httpModel.getData()); }}))Copy the code
  • For example, search function, hot search, if the previous interface has been requested, you can directly bring the data over, directly use, no longer adjust the interface to take. Or if there is no previous request and no cache, the interface is requested and the data is cached into the collection (memory cache).

Contains operator.

Determines whether an Observable emits a specific value and returns a Boolean result value. In RxJava, the contains operator determines whether a data set emits a specified event value.

  • For example, iterating through a list data set to determine whether an entry of a certain type exists or does not exist to do something.
Object model = ... Items items = new Items(); Observable.fromIterable(items) .contains(model) .as(RxLifecycleUtil.bindLifecycle(this)) .subscribe(new Consumer<Boolean>() { @Override public void accept(Boolean isContains) throws Exception { //... }});Copy the code