スケーラビリティの向上に役立つ多くのクエリを起動しないようにするには、クエリを起動する前に一定の時間待機するメカニズムを実装することをお勧めします。ユーザーがこの期間にフィールドの内容を変更すると、前のクエリを中断して新しいクエリをスケジュールします。
そのような何か:タイマーを作成し、タスクをスケジュール
コード:(いつでもユーザーの修正を起動する:前のスケジュールされたタスクをキャンセルする
Timer timer = new Timer();
// Schedule my task to be executed in 200 milliseconds
timer.schedule(new TimerTask() {
@Override
public void run() {
// Launch my query here
}
}, 200L);
コード何か)
// Cancel the previous timer which will also abort the scheduled task
timer.cancel();
// Create a new timer
timer = new Timer();
// Re-schedule the task
timer.schedule(new TimerTask() {
@Override
public void run() {
// Launch my query here
}
}, 200L);
で行うこともできますとして次:ScheduledExecutorService
を作成し、タスクをスケジュール
コード:(ユーザーが何かを変更する任意の時間に起動する)
:以前のスケジュールされたタスクをキャンセルする
// Create the ScheduledExecutorService
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
// Submit the task to be executed in 200 milliseconds
ScheduledFuture<?> future = executor.schedule(new Runnable() {
@Override
public void run() {
// Launch my query here
}
}, 200, TimeUnit.MILLISECONDS);
コード
// Cancel the task which will interrupt the thread that was executing the // task if any future.cancel(true); // Re-submit the task future = executor.schedule(new Callable<Void>() { @Override public Void call() throws InterruptedException { ... // Check regularly in your code if the thread has been // interrupted and if so throws an exception to stop // the task immediately if (Thread.currentThread().isInterrupted()) { throw new InterruptedException("Thread interrupted"); } ... } }, 200, TimeUnit.MILLISECONDS);
NB:これらのコードスニペットは、アイデアを示すためのものであり、私ではありませんアリは完全です
あなたのUI(モバイルまたはウェブ)は何ですか? –
あなたは本質的にすでにフィルタリングされた結果のキューを更新し、それに基づいて絞り込むだけのタスクを使用することができます(最新のクエリに基づいてキュー内に新規のものを配置します)。 – Rogue
@ShlomiHaverモバイルです。 Android –