【问题标题】:Android : Best Way to Call Retrofit API on TextChangeAndroid:在 TextChange 上调用 Retrofit API 的最佳方式
【发布时间】:2020-04-26 19:37:06
【问题描述】:

我创建了一个演示来使用 Retrofit 从服务器获取数据列表。我有 50000 多条记录。

现在我已经实现了,

  • 在 SearchView 中写入 3 个字符后调用 API
  • 之后,在每个字符按下事件上调用 API。

我面临的一个问题:

  • 加载项目太慢,
  • 每次按键时都有过多的 API 请求(任何使其更有效的解决方案)

我听说过Retrofit Caching,这可能对我有帮助,但对它了解不多。

任何其他提高效率的解决方案。

【问题讨论】:

  • After that, call API on each character press event. ,为什么?在发送请求之前,我会等到某种空闲时间。 I have 50000+ records.分页?
  • 您可以使用 Kotlin Coroutines 进行搜索。
  • @Blackbelt 是的,一种方法是等待一段时间并调用 API。主要问题是我必须从 50k+ 条记录中搜索它
  • 我认为我们有 RxJava。您可以在 RxJava 的帮助下同时完成所有工作。看看这篇文章medium.com/@kishankr.maurya/…
  • use Filter: 文档说 “通过调用 filter(CharSequence) 或 filter(CharSequence, android.widget.Filter.FilterListener) 执行的过滤操作是异步执行的。当这些方法被调用时,过滤请求将发布到请求队列中并稍后处理。对这些方法之一的任何调用都将取消任何先前未执行的过滤请求。"

标签: android web-services caching search retrofit


【解决方案1】:

这是我在 rxjava 中使用的

AutoCompleteTextView创建一个扩展函数

fun AutoCompleteTextView.addRxTextWatcher(): Observable<String?> {

    val flowable = Observable.create<String?> {
        addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

                it.onNext(s?.toString())
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }
        })
    }

    return flowable
}

对于AutocompleteTextView添加debounce策略,这里我添加了400毫秒的时间,如果400ms没有用户输入那么api请求就会去。根据您的要求更改时间

 autocompleteTextView.addRxTextWatcher()
            .debounce(400, TimeUnit.MILLISECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe {
                if (!TextUtils.isEmpty(it)) {
                    //DO api request
                }
            }

【讨论】:

    【解决方案2】:

    在java中我也认为最好的方法是使用RxJava和RxBinding如下

     compositeDisposable.add(RxTextView.textChangeEvents(searchEditText)
                .skipInitialValue()
                .debounce(300, TimeUnit.MICROSECONDS)
                .distinctUntilChanged()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<TextViewTextChangeEvent>() {
                    @Override
                    public void onNext(TextViewTextChangeEvent textViewTextChangeEvent) {
    
    adapter.getFilter().filter(textViewTextChangeEvent.getText());
                        Log.d(LOG_TAG, "The value seached "+ textViewTextChangeEvent);
                     //   adapter.notifyDataSetChanged();
                    }
    
                    @Override
                    public void onError(Throwable e) {
                        Log.d(LOG_TAG, "The error gotten from search: "+ e.getMessage());
                    }
    
                    @Override
                    public void onComplete() {
    
                    }
                }));
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-21
      • 1970-01-01
      • 2018-02-01
      • 2021-02-26
      • 2020-06-21
      • 2013-07-11
      • 2010-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多