Search code examples
androidkotlinrx-javarx-java2

How to cancel slow emission when using rxjava


I have implemented a search feature on Android. The issue occurring sometimes is that the initial search request triggers a network request that runs very slowly and eventually returns an error. In that time the user triggers another search by entering input into the search bar, which returns a success response and displays the search results to the user.

However, when the first result that took too long and failed returns, it's transformed into an empty result that clobbers the existing search results. Is there an operator in RxJava to possibly ignore this request that takes too long?


Solution

  • I fixed this issue by using switchMap to cancel the initial request. Before it was using flatMap.