4

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

MatPag
  • 41,742
  • 14
  • 105
  • 114
Anonymous-E
  • 827
  • 11
  • 29

1 Answers1

3

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*/ })
PPartisan
  • 8,173
  • 4
  • 29
  • 48