J
J
JZX2021-07-30 18:44:54
Android
JZX, 2021-07-30 18:44:54

How to handle error in switchMap?

There is a subject in which I send characters from EditText.

private val querySubject: BehaviorSubject<String> = BehaviorSubject.create()


Subscribe to it like this:

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)


A search request comes to this subject and the repository.search(it) repository method is called in the switchMap , which returns the search result as a Single with or . If the method returns an error, then it goes through the chain to subscribeBy and dispose() is called on the external emitter and the unsubscribe occurs and the reaction to the new input in the editText stops. How can you handle this type of error? I tried different methods, but the only thing that turned out was to call onErrorReturnItem(listOf(SEARCH_ERROR)) on the repository.search() method chain and return a list with a single element (SEARCH_ERROR string constant), by which I can understand that an error occurred in switchMap. List<String>Single< Exception>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-07-30
@JZX

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()
}

A couple of notes:
1. The subject is not needed here, in com.jakewharton.rxbinding2:rxbinding-kotlin there is a normal extension textChanges() for this
2. It is necessary to minimize the mutating of the external state from the rx-chain.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question