Answer the question
In order to leave comments, you need to log in
How to handle error in switchMap?
There is a subject in which I send characters from EditText.
private val querySubject: BehaviorSubject<String> = BehaviorSubject.create()
querySubject
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext {
viewState.showLoading()
currentQuery = it
}
.switchMap {
repository.search(it)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.toObservable()
}
.subscribe({
Log.i(TAG, it.toString())
if (currentQuery == EMPTY_STRING) {
_initialData = it
viewState.updateView(Success(null, initialData))
} else {
when {
it.isEmpty() -> {
viewState.updateView(Success(NOTHING_FOUND, it))
}
else -> {
val msg = FOUND_ITEMS_COUNT + it.size.toString()
viewState.updateView(Success(msg, it))
}
}
}
viewState.hideLoading()
}, {
viewState.showError(Error(SERVER_ERROR, it))
viewState.hideLoading()
})
.addTo(disposable)
List<String>
Single< Exception>
Answer the question
In order to leave comments, you need to log in
What else for Single<String>
or Single<Exception>
? It's a game. Do this: let the repository return Single<Result>
.
sealed class Result {
data class Success(result: String): Result()
data class Error(exception: Exception): Result()
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question