Rxjava Android如何使用Zip运算符 [英] Rxjava Android how to use the Zip operator

查看:432
本文介绍了Rxjava Android如何使用Zip运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为我的android项目理解RxJava中的zip运算符时遇到了很多麻烦。
问题
我需要能够发送网络请求上传视频
然后我需要发送一个网络请求来上传图片以便用它来
最后我需要添加说明并使用前两个请求的响应将视频和图片的位置网址以及说明上传到我的服务器。

I am having a lot of trouble understanding the zip operator in RxJava for my android project. Problem I need to be able to send a network request to upload a video Then i need to send a network request to upload a picture to go with it finally i need to add a description and use the responses from the previous two requests to upload the location urls of the video and picture along with the description to my server.

我认为zip操作符对于此任务是完美的,因为我知道我们可以接受两个可观察量(视频和图片请求)的响应并将它们用于我的最终任务。
但我似乎无法让我想象它会发生这种情况。

I assumed that the zip operator would be perfect for this task as I understood we could take the response of two observables (video and picture requests) and use them for my final task. But I cant seem to get this to occur how I envision it.

我正在寻找一个人来回答如何在概念上做一些伪代码。
谢谢

I am looking for someone to answer how this can be done conceptually with a bit of psuedo code. Thank you

推荐答案

Zip运算符严格配对来自observables的发射项。它等待两个(或更多)项目到达然后合并它们。所以是的,这将适合您的需求。

Zip operator strictly pairs emitted items from observables. It waits for both (or more) items to arrive then merges them. So yes this would be suitable for your needs.

我会使用 Func2 来链接前两个observable的结果。
请注意,如果使用Retrofit,这种方法会更简单,因为它的api接口可能会返回一个observable。否则你需要创建自己的observable。

I would use Func2 to chain the result from the first two observables. Notice this approach would be simpler if you use Retrofit since its api interface may return an observable. Otherwise you would need to create your own observable.

// assuming each observable returns response in the form of String
Observable<String> movOb = Observable.create(...);
// if you use Retrofit
Observable<String> picOb = RetrofitApiManager.getService().uploadPic(...),
Observable.zip(movOb, picOb, 

   new Func2<String, String, MyResult>() {

      @Override
      public MyResult call(String movieUploadResponse, 
          String picUploadResponse) {
            // analyze both responses, upload them to another server
            // and return this method with a MyResult type
          return myResult;
         }
      }
)
// continue chaining this observable with subscriber
// or use it for something else

这篇关于Rxjava Android如何使用Zip运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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