rxjava2-discussion icon indicating copy to clipboard operation
rxjava2-discussion copied to clipboard

关于显示loading的问题

Open cctanfujun opened this issue 7 years ago • 2 comments

我现在的需求网络请求如果请求结果在200毫秒内就不显示loading界面,如果请求超过200毫秒就显示loading界面,然后返回结果时候显示正常界面,用rx应该怎么实现比较好呢?

我现在的实现方式是把一个空数据和请求mergewith 但是我觉得这样非常不rx,想问问大家觉得怎么搞?

Observable<ValueBlock> loading = Observable.just(emptyList).delay(200, TimeUnit.MILLISECONDS);
        mAppMainModel.getMainData()
                .mergeWith(loading)
                .filter(valueBlock -> {
                    return (valueBlock.nav != null && valueBlock.nav.size() != 0) || mNavItems == null;
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ValueBlock>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(ValueBlock block) {
                        //nav数据
                        if (block.nav == null || block.nav.size() == 0) {
                            mView.showProgress("tt");
                        } else {
                            mNavItems = block.nav;
                            mView.setupViewPager(mNavItems, JSON.toJSONString(block.blocks));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        mView.hideProgress();
                    }

cctanfujun avatar May 02 '17 02:05 cctanfujun

@cctanfujun 可以试试timeout操作符。

tata8k avatar May 02 '17 12:05 tata8k

我觉得timeout并不能满足需求,从流图的角度讲我是 出现dialog和显示数据的两个事件我是都要处理的,和小伙伴讨论了一下用了如下方案,觉得还不错

public class LoadingTransformer<T> implements ObservableTransformer<T, T> {

    private static final String TAG = "LoadingTransformer";
    private Disposable mDisposable;
    private LoadDataView mLoadDataView;

    public LoadingTransformer(LoadDataView loadDataView) {
        mLoadDataView = loadDataView;
    }

    @Override
    public ObservableSource<T> apply(@NonNull Observable<T> upStream) {
        Observable.just(1).delay(200, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnDispose(() -> {
                    if (mLoadDataView != null) {
                        mLoadDataView.hideProgress();
                        mLoadDataView.setVisibility(View.GONE);
                    }
                })
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable disposable) {
                        mDisposable = disposable;
                    }

                    @Override
                    public void onNext(@NonNull Integer integer) {
                        if (mLoadDataView != null) {
                            mLoadDataView.setVisibility(View.VISIBLE);
                            mLoadDataView.showProgress("yy");
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable throwable) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
        return upStream
                .doOnNext(t -> {
                    if (!mDisposable.isDisposed()) {
                        mDisposable.dispose();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .doOnTerminate(() -> {
                    if (mLoadDataView != null) {
                        mLoadDataView.hideProgress();
                        mLoadDataView.setVisibility(View.GONE);
                    }
                });
    }
}

compse 这个

cctanfujun avatar May 02 '17 13:05 cctanfujun