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
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With