Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rxjava Android how to use the Zip operator

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.

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

like image 810
feilong Avatar asked May 13 '15 15:05

feilong


People also ask

What is zip in RxJava?

As per the RxJava official documentation, Zip combine the emissions of multiple Observables together via a specified function and emit single items for each combination based on the results of this function. Zip operator allows us to get the results from multiple observables at a time.

What does zip do in RxJS?

RxJS implements this operator as zip and zipArray . zip accepts a variable number of Observables or Promises as parameters, followed by a function that accepts one item emitted by each of those Observables or resolved by those Promises as input and produces a single item to be emitted by the resulting Observable.

How does RxJava work on Android?

RxJava is a JVM library that uses observable sequences to perform asynchronous and event-based programming. Its primary building blocks are triple O's, which stand for Operator, Observer, and Observables. And we use them to complete asynchronous tasks in our project. It greatly simplifies multithreading in our project.

What is RX Observable?

There are two key types to understand when working with Rx: Observable represents any object that can get data from a data source and whose state may be of interest in a way that other objects may register an interest. An observer is any object that wishes to be notified when the state of another object changes.


1 Answers

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.

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 
like image 81
inmyth Avatar answered Sep 23 '22 15:09

inmyth