RX-Android + ViewModel + Retrofit不调用OnComplete()

时间:2019-03-02 23:17:10

标签: mvvm rx-java retrofit2 reactive-programming rx-android

处理完所有项目后,我无法调用OnComplete()方法。我需要这样做(至少)是隐藏加载视图。我对JavaRX有点陌生,所以我不知道问题出在哪里。您可以帮我在处理所有项目时调用OnComplete()吗?

代码执行以下操作:

  1. 显示加载视图并获取项目列表(仅供参考)。
  2. 检查它们是本地项目还是远程项目。
  3. 如果它们是本地的,请获取它们并将其添加到列表中。
  4. 如果它们是远程的,请下载它们并将其添加到列表中。
  5. 构建列表后,在UI上绘制数据。
  6. 最终处理和隐藏加载视图。

代码如下:

private void loadDataRX(final long fromTime, final long toTime) {
    mLoadingPb.setVisibility(View.VISIBLE);
    iCompositeDisposable.clear();
    iCompositeDisposable.add(mViewModel.getItems(fromTime, toTime)
            .subscribeOn(Schedulers.io())
            .flatMap(items -> {
                Activity context = ItemFragment.this.getActivity();
                if (context == null) {
                    Log.e(TAG, "Cannot present results: context is null");
                    return Flowable.empty();
                } else {
                    context.runOnUiThread(() -> {
                        mItems.clear();
                        mCustomView.reset();
                    });
                    if (items != null && items.size() > 0) {
                        return Flowable.just(items);
                    } else {
                        Log.i(TAG, "No items.");
                        return Flowable.just(Collections.singletonList(new Item(-1))); // This is my current way of solving a similar problem so as to know if I don't have any items
                    }
                }
            })
            .concatMapIterable(items -> items)
            .concatMap(item -> {
                if (item.getUid() == -1) {
                    return Flowable.just(item);
                }
                String file = item.getFileName();
                boolean uploaded = item.isUploaded();
                if (uploaded) { // Remote file
                    if (item.getUid() > 0) {
                        return iRetrofit.create(RestApi.class).getItem(item.getUid());
                    } else {
                        return Flowable.empty();
                    }
                } else { // Local file
                    return Flowable.just(item);
                }
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(item -> {
                Log.i(TAG, "Loaded items RX");
                if (item instanceof Item) {
                    //Do stuff with the item and the files
                } else if (item instanceof ResponseBody) {
                    //This is dirty but I didn't find another way. So here I basically extract the items and the files from the server's response. At least, it works.
                } else {
                    Log.i(TAG, "No results for the given dates");
                }
            }, throwable -> {
                mLoadingPb.setVisibility(View.GONE);
                Log.e(TAG, "Error: " + throwable.getMessage());
            }, () -> {
                mLoadingPb.setVisibility(View.GONE);
                Log.i(TAG, "Loading results completed"); // Can't get this to be called
            })
    );
}

谢谢。

1 个答案:

答案 0 :(得分:1)

我猜想mViewModel.getItems返回Flowable。为了使流动性完整,我们需要明确处理它。

要解决此问题,您可以使mViewModel.getItems返回Single<List<ItemType>>,然后使用.flatMapObservable { Observable.fromIterable(it) }转换流以处理每个项目。