I\'m using RxJava to iterate over a list of files, making a network call to upload each file, then collect the files that were successfully uploaded in a list and persist th
Here is how I have handled it in the past by using the zip
method.
// create an observable list that you can process for you file uploads
val responses: Response = listOf()
queryFiles()?.let { file ->
val observable = Observable.create(ObservableOnSubscribe { emitter ->
// you can modify this section to your data types
try {
// with your uploadFile method you might be able to just add them
// all the responses list
emitter.onNext(uploadFile(file))
emitter.onComplete()
} catch (e: Exception) {
emitter.onError(e)
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
responses.add(observable)
}
// i setup a simple booleanArray to handle success/failure but you can add
// all the files that fail to a list and use that later
val isSuccessful = booleanArrayOf(true)
Observable.zip(responses, Function, Boolean> { responses ->
var isSuccessful: Boolean? = java.lang.Boolean.TRUE
// handle success or failure
isSuccessful
}).subscribe(Consumer { aBoolean -> isSuccessful[0] = aBoolean!! }, Consumer { throwable ->
isSuccessful[0] = false
}, Action {
// handle your OnComplete here
// I would check the isSuccessful[0] and handle the success or failure
})
This is creating all your uploads into a list of Observables that can be handled and merged with the zip
method. This will merge them all when they are done into an array of any so that you can loop over them - your result from the uploadFile() method. This example is checking for a success or failure from the responses that come back. I removed most of the logic where the comment // handle success or failure
is. In the function method you can keep track of your file uploads that fail or succeed.