本文介绍了RxJava运算符Debounce无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想在Android应用程序中实现地点自动完成功能,为此,我正在使用Retrofit和RxJava。我想在用户输入内容后每2秒做出一次响应。我正在尝试为此使用反跳运算符,但是它不起作用。它立即为我提供了结果,而没有任何暂停。I want to implement place autocomplete in Android application, and for this I'm using Retrofit and RxJava. I want to make response every 2 seconds after user type something. I'm trying to use debounce operator for this, but it's not working. It's giving me the result immediately without any pause. mAutocompleteSearchApi.get(input, "(cities)", API_KEY) .debounce(2, TimeUnit.SECONDS) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .flatMap(prediction -> Observable.fromIterable(prediction.getPredictions())) .subscribe(prediction -> { Log.e(TAG, "rxAutocomplete : " + prediction.getStructuredFormatting().getMainText()); });推荐答案正如@BenP在评论中所说,您似乎正在对放置自动填充功能去抖动 / a>服务。该调用将返回一个Observable,该Observable在完成之前发出单个结果(或错误),此时 debounce 运算符将发出该唯一的项目。As @BenP says in the comment, you appear to be applying debounce to the Place Autocomplete service. This call will return an Observable that emits a single result (or error) before completing, at which point the debounce operator will emit that one and only item.您可能想做的是用类似以下的命令来消除用户输入:What you probably want to be doing is debouncing the user input with something like:// Subject holding the most recent user inputBehaviorSubject<String> userInputSubject = BehaviorSubject.create();// Handler that is notified when the user changes inputpublic void onTextChanged(String text) { userInputSubject.onNext(text);}// Subscription to monitor changes to user input, calling API at most every// two seconds. (Remember to unsubscribe this subscription!)userInputSubject .debounce(2, TimeUnit.SECONDS) .flatMap(input -> mAutocompleteSearchApi.get(input, "(cities)", API_KEY)) .flatMap(prediction -> Observable.fromIterable(prediction.getPredictions())) .subscribe(prediction -> { Log.e(TAG, "rxAutocomplete : " + prediction.getStructuredFormatting().getMainText()); }); 这篇关于RxJava运算符Debounce无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-16 00:48