Allan Araújo
Allan Araújo

Reputation: 173

Waiting for API Calls to complete with RxJava and Retrofit

I'm using RxAndroid and RxJava with Kotlin to make various requests to APIs and reciving them asyncronylusly:

    getClient().sendSomeData(data)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    {
                        Log.i("upload", "data ok")
                    },
                    {
                        t ->
                        Log.e("upload", "data error: " + t.stackTrace)
                    }
            );


    getClient().sendSomeOtherData(otherData)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    {
                        Log.i("upload", "otherData ok")
                    },
                    {
                        t ->
                        Log.e("upload", "data error: " + t.stackTrace)
                    }
            );

And so on. What i want to do now is, sort of "wait" for all the api requests to finish and show the progress. How can i wait for all observables to be completed and get a callback with progress?

Upvotes: 1

Views: 1270

Answers (1)

Jakub Licznerski
Jakub Licznerski

Reputation: 1088

Answering your first question here and here you will see mentioned the Observable.onCompleted method, which is called after succesful response. Also here is the doc for the this method. So you could simply implement the third Action in subscribe method call and modify your request's state info.

If it comes to the request progress status, I'm not sure, but I suppose that RxJava does not offer this functionality out of the box. However I found the answer here (with use of Retrofit library).

Upvotes: 1

Related Questions