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

问题描述 投票:0回答:1

在处理完所有项目后,我无法调用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
            })
    );
}

提前致谢。

mvvm rx-java retrofit2 reactive-programming rx-android
1个回答
1
投票

我想mViewModel.getItems会回归Flowable。要使flowable完成,我们需要明确处理它。

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

© www.soinside.com 2019 - 2024. All rights reserved.