【发布时间】:2020-10-29 08:58:58
【问题描述】:
我正在使用 RxJava 为我的 Android 应用程序构建搜索功能。当用户在EditText 中键入/更改查询字符时,发射器将发出新的查询文本,我在我的数据库中搜索以获取与查询匹配的结果。
getEditTextObservable()
.subscribeOn(Schedulers.computation())
.debounce(500, TimeUnit.MILLISECONDS)
.filter {
!it.isNullOrEmpty()
}
.map {
//start searching
getResultsInDatabase(it) //this function takes a long time to complete
}.observeOn(AndroidSchedulers.mainThread())
.subscribe{
//render results on screen
}
方法getResultsInDatabase(string:String) 需要很长时间才能完成,当查询文本更改时,我想停止方法getResultsInDatabase(string:String)(以防它与先前发出的查询一起运行)以使用新查询再次运行它。我该怎么做才能做到这一点?我会很感激你的帮助。感谢您阅读我的问题。
【问题讨论】: