簡體   English   中英

如果RxJava observable需要很長時間,你如何顯示微調器?

[英]How do you show spinner if RxJava observable takes to long?

我覺得有人必須嘗試這個,但如果一個觀察者需要很長時間,我無法想出一個很好的方法來做某事。

這是我想要的流程。

Start a search.
If the search takes longer than some time,
    show a spinner or show progress bar.
When finished do subscription action and hide spinner/progress bar.

我能想到的最接近的就像一個Zip

manager.search(searchTerm)
       .zip(Observable.Timer(1, TimeUnit.SECONDS))
       .subscribe(
           // if the search is non null then we are good
           // if the long time is non 0 we need to show spinner
       );

還有更好的事嗎? 我整天都在努力,沒有成功。 在一個完美的世界里,我覺得我會想要這樣的東西

manager.search(searchTerm)
       .timeout(i -> /* do timeout stuff */, 1, TimeUnit.SECONDS)
       .subscribe(item -> /* do search result stuff */);

您可以通過超時發布搜索Observable來完成此操作:

Observable<Integer> source = Observable.just(1).delay(5, TimeUnit.SECONDS);

source
.doOnSubscribe(() -> System.out.println("Starting"))
.publish(o -> 
    o.timeout(1, TimeUnit.SECONDS, Observable.<Integer>fromCallable(() -> {
        System.out.println("Spinning...");
        return null;
    })).ignoreElements().mergeWith(o)
)
.toBlocking()
.subscribe(v -> {
    System.out.println("Hide spinner if shown.");
    System.out.println(v);
});

這可以通過將源分成兩個熱通道來實現:第一個通道將運行一個timeout運算符,當timeout時,啟動另一個具有顯示旋轉控件的副作用的Observable。 其中一種方法是使用fromCallable並忽略其結果(這也避免重復)。 第二個通道將保持不變並與超時通道合並以提供實際值。

今天我發現有點奇怪但有效的解決方案。 想法是使用間隔而不是計時器。

    fun <T> base_delayed_progress_observable(source: Observable<T>): Observable<T>
    {
        val timer = Observable.interval(100, TimeUnit.MILLISECONDS) //Creates observable that will emit Long++ each 100 miliseconds
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(
                    {
                        if (it == 10L)//Here we check current timer value. For example here i check if it is 1 second gone (100 miliseconds * 10 = 1 second)
                        {
                            //here we put all we need to show progress/spinner an so on
                        }
                    })

        return Observable.zip(source, timer,
            BiFunction<T, Long, T> { t1, t2 ->
                //Here we return our original Obervable zipped with timer
                //Timer will be cancelled when our source Observable gets to OnComplete
                return@BiFunction t1
            }).doFinally(
            {
                //Here we can dismiss all progress dilogs/spinner
            })
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM