gpt4 book ai didi

android - RxJava 运算符 Debounce 不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 11:46:43 25 4
gpt4 key购买 nike

我想在 Android 应用程序中实现位置自动完成功能,为此我使用了 Retrofit 和 RxJava。我想在用户输入内容后每 2 秒做出响应。我正在尝试为此使用去抖动运算符,但它不起作用。它立即给我结果,没有任何停顿。

 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 在评论中所说,您似乎正在对 Place Autocomplete 应用 debounce服务。此调用将返回一个 Observable,该 Observable 在完成之前发出单个结果(或错误),此时 debounce 运算符将发出该唯一项。

你可能想要做的是用类似的东西去抖动用户输入:

// Subject holding the most recent user input
BehaviorSubject<String> userInputSubject = BehaviorSubject.create();

// Handler that is notified when the user changes input
public 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());
});

关于android - RxJava 运算符 Debounce 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47275287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com