RxJava-链​​接请求和更新UI [英] RxJava - Chaining requests and updating UI

查看:63
本文介绍了RxJava-链​​接请求和更新UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是这个.我需要执行几个对服务器的请求.每个下一个请求取决于上一个请求的结果.它们看起来像这样(缩写):

The problem I'm having is this. I need to execute couple of requests to the server. Each next request depends on the result of previous one. They look like this (abbreviated):

Observable<FileUploadResponse> obsFile = api.uploadFile();
Observable<TokenCreateResponse> obsCreateToken = api.createToken();
Observable<PaymentResponse> obsPayment = api.submitOrder();

我已经使用flatMap创建了一个可观察的对象,该对象可返回PaymentResponse对象,或者如果不满足某些要求,则会发出onError().一切正常,我可以在一次调用中完成所有请求.

I've created a single observable using a flatMap which returns the PaymentResponse object or emits onError() if some of the requirements aren't met. This is working fine and I get all the requests done in a single call.

问题是我无法在这些请求之间更新UI.在当前设置下,我将在请求启动时显示加载,并在所有请求完成时将其隐藏.有没有办法在这些请求之间更新UI?

The problem is I can't update the UI between these requests. With current setup, I show the loading when request starts and hide it when all the requests are complete. Is there a way to update the UI in-between these requests?

我想要的是:1.文件上传-在UI上写一条消息.2.创建令牌-在UI上写一条消息.3.提交订单-在UI上写一条消息.4.完成所有步骤后,隐藏进度对话框.

What I want is this: 1. File uploading - write a message on the UI. 2. Creating a token - write a message on the UI. 3. Submitting order - write a message on the UI. 4. Once all are complete, hide the progress dialog.

我的理解是在每个API调用完成时使用onNext()发出一些Observable,然后在完成所有操作时调用onComplete().但是我该怎么办呢?

My understanding would be to emit some Observable using onNext() when each API call is finished and then calling onComplete() when all are done. But how do I do this?

推荐答案

您可以使用 doOnNext PublishSubject 来实现.首先创建一个主题和一些值:

You could achieve this with doOnNext and a PublishSubject. First create a subject and some values:

public static final int STATUS_UPLOADING = 0;
public static final int STATUS_TOKEN = 1;
public static final int STATUS_SUBMITTING = 2;
public static final int STATUS_DONE = 3;

PublishSubject<Integer> status = PublishSubject.create();

public Observable<Integer> getStatusStream() {
    return status;
}

然后,当您执行上载工作时,只需将值每次都发送给主题:

Then when you're doing your upload work just send the value to the subject each time:

status.onNext(STATUS_UPLOADING);

return api.uploadFile()
    .doOnNext(o -> status.onNext(STATUS_TOKEN))
    .flatMap(o -> api.createToken())
    .doOnNext(o -> status.onNext(STATUS_SUBMITTING))
    .flatMap(o -> api.submitOrder())
    .doOnNext(o -> status.onNext(STATUS_DONE))

然后,您可以订阅主题并更新您的UI:

Then you can subscribe to the Subject and update your UI:

model.getStatusStream()
    .subscribeOn(AndroidSchedulers.mainThread())
    .subscribe(
        status -> {
            view().setMessage(status);
        },
        Throwable.printStackTrace
    );

或者,根据您想要构建应用程序的方式,您可以每次仅通过 doOnNext 调用更新视图调用.您可能需要使用 observeOn 在主&每次都有后台线程.

Alternatively depending on how you want to architect your app you could just call the update view calls from doOnNext each time. You'd probably need to use observeOn to switch between the main & background thread each time.

这篇关于RxJava-链​​接请求和更新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆