I have a list of users and I need to save users one by one via API call. I tried the below code and it sends the same user for all API calls.
gradle
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxkotlin:2.4.0'
kotlin
data class User(val name: String, val age: Int)
fun saveUsers(users: MutableList<User>) {
var index = 0
compositeDisposable.add(userRepository.saveUser(users[index])
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe {
// do something
}.doFinally {
// do something
}.repeatWhen { it.delay(1, TimeUnit.SECONDS) }
.repeatUntil {
users.size -1 == index
}.subscribe({
if ( users.size -1 == index) {
// do something
} else {
index++
// do something
}
}, {
it.printStackTrace()
}))
}
userRepository.saveUser()
method returns Single<ResultResponse>
class ResultResponse(
val ResultCode: Int = -1
)
Can you please give me a solution for these multiple API calls?