简体   繁体   English

在RxJava中使用else语句

[英]Using else statements in RxJava

The code below is simply checking null control and make mView.handleUrl(data) method work if all conditions are provided. 下面的代码只是检查空控件,如果提供了所有条件,则使mView.handleUrl(data)方法起作用。

if( intent != null && intent.getExtras() != null ){
        String data = null;
        if( ( data = intent.getExtras().getString( AdvImagePagerFragment.ARG_PANAROMIC_IMAGE_DATA ) ) != null ){
            mView.handleUrl( data );
        }
    }

When I use code with RxJava I write code below. 当我在RxJava中使用代码时,我在下面编写代码。

 Single.just( intent )
                .filter( intent1 -> intent1 != null && intent1.getExtras() != null )
                .map( intent12 -> intent12.getExtras() )
                .filter( bundle -> bundle.getString( AdvImagePagerFragment.ARG_PANAROMIC_IMAGE_DATA  ) != null )
                .map( bundle -> bundle.getString( AdvImagePagerFragment.ARG_PANAROMIC_IMAGE_DATA ) )
                .subscribe( s -> mView.handleUrl( s ) );

Both code I write are working same. 我写的两个代码都一样。 However the problem is starting when I try to add new condition to my code. 但是,当我尝试向代码中添加新条件时,问题就开始了。 For example, I can want to make some new operatins if the intent is null. 例如,如果意图为null,我可能要进行一些新操作。

Example code; 示例代码;

if( intent != null && intent.getExtras() != null ){
        String data = null;
        if( ( data = intent.getExtras().getString( AdvImagePagerFragment.ARG_PANAROMIC_IMAGE_DATA ) ) != null ){
            mView.handleUrl( data );
        } else { // START Added a new condition
           mView.showError();
        }
    }

But I cannot write this code by using RxJava filter methods. 但是我无法使用RxJava过滤器方法编写此代码。 As soon as filter return false, the operation is being completed. 过滤器返回false时,操作即告完成。 What is the solution in RxJava to add new conditions. RxJava中添加新条件的解决方案是什么。

As a subscriber to that stream you are interested for 2 cases: if data is null and if data is not null. 作为该流的订户,您有两种情况感兴趣:如果data为null和data不为null。 That means, that you cannot filter out the stream just like you have done previously. 这意味着,您不能像以前一样filter掉流。


    Single.just(sourceIntent)
            .map(intent -> {
                String data = "";
                Bundle bundle = intent.getExtras();
                if (bundle != null) {
                    data = bundle.getString(KEY, "");
                }
                return data;
            })
            .subscribe(data -> {
                if ("".equals(data)) view.showError();
                else view.handleUrl(data);
            });

Try this :- 尝试这个 :-

Single.just( intent )
            .filter( intent1 -> intent1 != null && intent1.getExtras() != null )
            .map( intent12 -> intent12.getExtras() )
            .map( bundle -> bundle.getString( AdvImagePagerFragment.ARG_PANAROMIC_IMAGE_DATA ) )
            .map(string -> string == null ? "show_error" : string)
            .subscribe(new DisposableMaybeObserver<String>() {
                @Override
                public void onSuccess(String s) {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            })

onNext(String s) {if (s.equalsIgnoreCase("show_error")) { //handle error }} like this:- onNext(String s) {if (s.equalsIgnoreCase("show_error")) { //handle error }}像这样:

.subscribe(new DisposableMaybeObserver<String>() {
                @Override
                public void onSuccess(String s) {
                     if (s.equalsIgnoreCase("show_error")) {
                      //handle error 
                    mView.showError();
                    }
                    else
                     {
                           mView.handleUrl( data );
                        }
                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            });

Hope this helps . 希望这可以帮助 。

To split an observable into multiple observables based on some condition you should use groupBy operator . 要根据某种条件将一个可观察对象拆分为多个可观察对象,应使用groupBy 运算符

Here is a nice example . 这是一个很好的例子

In your case you don't need it since you want to show an error and there is a separate mechanism for errors in Rx. 在您的情况下,您不需要它,因为您想显示错误,并且在Rx中有单独的错误处理机制。

You can do something like: 您可以执行以下操作:

Single.just( intent )
            .filter( intent1 -> intent1 != null && intent1.getExtras() != null )
            .map( intent12 -> intent12.getExtras() )
            .flatMap( bundle -> 
              String imageData = bundle.getString( AdvImagePagerFragment.ARG_PANAROMIC_IMAGE_DATA);
              if (imageData != null) {
                return Observanle.just(imageData); 
              } else {
                return Observable.error(new RuntimeException("something is wrong));
              }
            ).subscribe(s -> mView.handleUrl( s ),
                        error -> mView.showError( error ));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM