gpt4 book ai didi

android - 如何使用 Rx 和 Retrofit 仅发送最后一个请求?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:18:59 32 4
gpt4 key购买 nike

我有一个 EditText View 和它的 TextWatcher,在 onTextChanged 方法中,我必须请求服务器获取来自 EditText 字段的查询结果。在我的演示者中,我为此使用 rx,但我需要延迟搜索,直到用户输入结束。此刻我得到了这个:

service.getData(query)
.delaySubscription(REQUEST_DELAY_FROM_SERVER, TimeUnit.MILLISECONDS, Schedulers.io())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
data-> {
getViewState().showData(data);
},
error -> {
Log.e(this.getClass().getSimpleName(), error.getMessage(), error);
}
);

但是 delaySubscription 没有按预期工作。它收集所有调用并在延迟后发送每个调用。我必须像使用 handler.postDelayed() 那样做,此时只发送一次请求。

最佳答案

编辑 2:

RxJava2 中演示者的 saple

class Presenter {
private PublishSubject<String> queryPublishSubject = PublishSubject.create();

public Presenter() {
queryPublishSubject
.debounce(1000, TimeUnit.MILLISECONDS)
// You might want to skip empty strings
.filter(new Predicate<CharSequence>() {
@Override
public boolean test(CharSequence charSequence) {
return charSequence.length() > 0;
}
})
// Switch to IO thread for network call and flatMap text input to API request
.observeOn(Schedulers.io())
.flatMap(new Function<CharSequence, Observable<...>() {
@Override
public Observable<...> apply(final CharSequence charSequence) {
return ...; // Call API
}
})
// Receive and process response on Main thread (if you need to update UI)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(...);
}

public void onSearchTextChanged(String query) {
queryPublishSubject.onNext(query);
}
}

编辑 1:

RxJava 1中的相同代码:

class Presenter {
private PublishSubject<String> queryPublishSubject = PublishSubject.crate();

public Presenter() {
queryPublishSubject
.debounce(1000, TimeUnit.MILLISECONDS)
// You might want to skip empty strings
.filter(new Func1<CharSequence, Boolean>() {
@Override
public Boolean call(CharSequence charSequence) {
return charSequence.length() > 0;
}
})
// Switch to IO thread for network call and flatMap text input to API request
.observeOn(Schedulers.io())
.flatMap(new Func1<CharSequence, Observable<...>() {
@Override
public Observable<...> call(final CharSequence charSequence) {
return ... // Call API
}
})
// Receive and process response on Main thread (if you need to update UI)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(...);
}

public void onSearchTextChanged(String query) {
queryPublishSubject.onNext(query);
}
}

初始答案(使用 RxBinding 和 RxJava 1)

正确的答案是使用Debounce,但除此之外还有一些您可能会觉得有用的其他技巧

textChangeListener = RxTextView
.textChanges(queryEditText)
// as far as I know, subscription to textChanges is allowed from Main thread only
.subscribeOn(AndroidSchedulers.mainThread())
// On subscription Observable emits current text field value. You might not need that
.skip(1)
.debounce(1000, TimeUnit.MILLISECONDS)
// You might want to skip empty strings
.filter(new Func1<CharSequence, Boolean>() {
@Override
public Boolean call(CharSequence charSequence) {
return charSequence.length() > 0;
}
})
// Switch to IO thread for network call and flatMap text input to API request
.observeOn(Schedulers.io())
.flatMap(new Func1<CharSequence, Observable<...>() {
@Override
public Observable<...> call(final CharSequence charSequence) {
return ... // Call API
}
})
// Receive and process response on Main thread (if you need to update UI)
.observeOn(AndroidSchedulers.mainThread())

关于android - 如何使用 Rx 和 Retrofit 仅发送最后一个请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42503368/

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