How to check if RXJava finish or empty from queue

Anonymous-E :

I have a function that loop the request using Retrofit and RXJava as below

for (i in month..12) {
    if (Conn.connection(applicationContext)) {
        Api.create().getDateInMonth("2019-$i-01")
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<DateDataResponse> {
                override fun onSubscribe(d: Disposable) {}

                @SuppressLint("NewApi")
                override fun onNext(dateDataResponse: DateDataResponse) {
                    Log.d("OnSuccess", "success")
                }
                @SuppressLint("NewApi")
                override fun onError(e: Throwable) {
                    Log.d("onError", "error" + e.message)
                }

                override fun onComplete() {
                    Log.d("onComplete", "onComplete")
                }
            })
    } else
        Log.d("onError", "No Internet Connection")
    }
}

so if some request error or success it will go on until the 12 request is finish. I want to detect if I already got all response from my request

PPartisan :

If you turn this into a single chain, then you can use the onComplete() callback to verify that all your requests have finished. For example:

Observable.range(0, 12)
    .filter { i-> Conn.connection(applicationContext) }
    .flatMap { i -> Api.create().getDateInMonth("2019-$i-01") } 
    .subscribeOn(io())
    .observeOn(mainThread())
    .subscribe({ i-> }, { t-> }, {/*onComplete*/ })

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=126531&siteId=1