RxJava链接两个Completables

时间:2018-02-22 10:52:49

标签: android rx-java rx-java2

我是RxJava的新手并且有问题。我尝试链接两个Completables。所以场景是更新一些类别对象,然后更新UI。这两项任务都将在Completable中执行。我使用andThen运算符链接了这两个,但有时UI Completable没有执行,我也没有收到任何错误。

        Disposable d = FilterController.getInstance().updateChosenCategoriesCompletable()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .andThen(FilterController.getInstance().updateChosenCategoriesInterfaceCompletable())
            .subscribeWith(new DisposableCompletableObserver() {
                @Override
                public void onComplete() {
                    Log.d("MultiselectDialog", "Categories Update Success");
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                    ExceptionHandler.saveException(e, null, null);
                    Log.d("MultiselectDialog", "error");
                }
            });

我不确定,但我认为这是由IO线程上执行第一个Completable引起的。当我尝试链接两个Completables时这是禁止的吗?关于它的奇怪之处在于第二个Completable有时不会执行(例如10个中的1个)。当我插入一个延迟或在主线程上做所有事情然后一切正常。我只是想知道它是否被禁止用两个不同的线程链接两个Completables。

提前感谢您的帮助!

/编辑

updateChosenCategoriesCompletable()正在使用此Completable:

public Completable setChosenCategoryCompletable(final ArrayList<CategoryHolder> category, final boolean callFilterOptionsChanged) {
    return Completable.fromAction(new Action() {
        @Override
        public void run() throws Exception {
            try {
                Log.d("Workflow", "setChosenCategoryCompletable " + category.size());
                Log.d("Workflow", "setChosenCategoryCompletable " + Thread.currentThread().getName());
                mChosenCategories = category;

                if (callFilterOptionsChanged) {
                    mFilterOptionsChangedListener.filterOptionsChanged();
                }
            } catch (Exception e){
                Log.d("Workflow", "error set");
                e.printStackTrace();
            }
        }
    });
}

并且updateChosenCategoriesCompletable()正在使用此Completable

    public Completable updateCategoryLabelTextCompletable(final ArrayList<CategoryHolder> categories) {
    return Completable.fromAction(new Action() {
        @Override
        public void run() throws Exception {
            Log.d("Workflow", "updateCategoryLabelTextCompletable " + categories.size());
            Log.d("Workflow", "updateCategoryLabelTextCompletable " + Thread.currentThread().getName());
            TextView selectedValue = mLabels.get(mActivity.getString(R.string.CATEGORY));
            ImageView categoryIcon = (ImageView) mRootView.findViewById(R.id.category_icon);
            if(categories.size() > 1) {
                selectedValue.setText(String.valueOf(categories.size()));
                categoryIcon.setVisibility(View.GONE);
            }
            else if(categories.isEmpty()) {
                selectedValue.setText(mActivity.getString(R.string.CATEGORY_EMPTY));
                categoryIcon.setVisibility(View.GONE);
            }
            else {
                selectedValue.setText(categories.get(0).getCategory());

                // Set category image if only one category is selected
                categoryIcon.setVisibility(View.VISIBLE);
                String categoryImage = categories.get(0).getCategoryImage();
                if(!categoryImage.isEmpty()){

                    int imageResource = mActivity.getResources().getIdentifier(categoryImage, "drawable", mActivity.getPackageName());

                    Drawable image = null;

                    // getDrawable(int) is deprecated since API 21
                    try {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            image = mActivity.getResources().getDrawable(imageResource, null);
                        } else {
                            image = mActivity.getResources().getDrawable(imageResource);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                    if(image != null){
                        categoryIcon.setImageDrawable(image);
                    }
                }else {
                    categoryIcon.setVisibility(View.GONE);
                }
            }
        }
    });
}

是否必须使用fromCallable?

这是更新我提到的按钮的方法:

    public void setResultButtonText(final String text, final boolean buttonEnabled) {

    if(mShowResultsButton != null) {

        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    mShowResultsButton.setText(text);
                    mShowResultsButton.setEnabled(buttonEnabled);
                    if (buttonEnabled) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            mShowResultsButton.setBackground(mActivity.getDrawable(R.drawable.ripple));
                        } else {
                            mShowResultsButton.setBackground(mActivity.getResources().getDrawable(R.drawable.ripple));
                        }
                    } else {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            mShowResultsButton.setBackground(mActivity.getDrawable(R.drawable.ripple_grey));
                        } else {
                            mShowResultsButton.setBackground(mActivity.getResources().getDrawable(R.drawable.ripple_grey));
                        }
                    }
                } catch (Exception e){
                    e.printStackTrace();
                    Log.d("Filterinterface", "ERROR");
                }
            }
        });
    }
}

0 个答案:

没有答案