2017-11-13 34 views
0

私はAndroidアプリケーションでプレースオートコンプリートを実装したいと思いますが、これはRetrofitとRxJavaを使用しています。私はユーザーが何かを入力した後に2秒ごとに応答したい。私はこれに対してdebounce演算子を使用しようとしていますが、動作しません。それはすぐに結果を私に与えています。 @BenPコメントで言うようにRxJavaオペレータDebounceが機能しない

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()); 
      }); 
+0

あなたがネットワーク呼び出しをデバウンスしているように見えます:あなたはおそらくやって欲しい

のようなもので、ユーザの入力をデバウンスされます。おそらく、代わりにユーザー入力イベントをデバウンする必要があります。 –

答えて

2

は、あなたがPlace Autocompleteサービスへdebounceを適用するように見えます。この呼び出しは、完了する前に単一の結果(またはエラー)を出す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()); 
    }); 
関連する問題