简体   繁体   English

使用rxjava计时器和改造

[英]Using rxjava timer and retrofit

I know this code is antipatterns but how can I write it correctly. 我知道此代码是反模式,但如何正确编写。 Can you help me? 你能帮助我吗? If the web api does not respond in 2 seconds, I want to open a ProgressDialog. 如果Web API在2秒钟内没有响应,我想打开一个ProgressDialog。 When you answer, close it. 当您回答时,将其关闭。

getCompositeDisposable().add(mRemoteUseCase.sendData(profileInfo)
                .subscribeOn(scheduler)
                .observeOn(androidSchedulers)
                .doOnSubscribe(s ->
                {
                    mIsLoading.set(true);
                    Observable.timer(2000, TimeUnit.MILLISECONDS).take(1).subscribeOn(scheduler)
                            .observeOn(androidSchedulers).doOnNext(c -> {
                        if (mIsLoading.get() && isShowProgressDialog) {
                            progressDialog.show();
                        }
                    }).subscribe(l -> {

                    });
                })
                .doAfterTerminate(() -> {
                    mIsLoading.set(false);
                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }
                })
                .subscribe(new Consumer<RemoteResponse<UserData>>() {
                               @Override
                               public void accept(RemoteResponse<UserData> remoteResponse) throws Exception {

                                   dataManager.setProfile(profile);

                               }
                           },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                listener.onError(throwable);

                                dialog = DialogFactory.createSimpleOkErrorDialog(context, "Error");
                                dialog.show();
                            }
                        })
        );

You can use two observables. 您可以使用两个观察值。 The first one will use .delay() to show the progress dialog. 第一个将使用.delay()显示进度对话框。 The second will you'll use to make the request. 第二个将用于发出请求。 If the request finishes before 2 seconds - cancel the first one with .dispose() . 如果请求在2秒之前完成,请使用.dispose()取消第一个.dispose()

Here's an example (it's Kotlin but you'll get the idea): 这是一个示例(是Kotlin,但您会明白的):

    val dialogObservable = Single.just(true)
            .observeOn(Schedulers.computation())
            .subscribeOn(AndroidSchedulers.mainThread())
            .delay(2, TimeUnit.SECONDS)
            .subscribe({
                // show dialog
            }, {
                // do something with the error
            })

    val startTime = System.currentTimeMillis()
    mRemoteUseCase.sendData(profileInfo)
            .subscribeOn(scheduler)
            .observeOn(androidSchedulers)
            .subscribe({
                val endTime = System.currentTimeMillis()
                if(TimeUnit.MILLISECONDS.toSeconds(endTime - startTime) <= 2){
                    dialogObservable.dispose()
                }

                // do something else
            }, {
                // do something with the error
            })

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

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