簡體   English   中英

rxjava中的throttleLatest和throttleLast有什么區別?

[英]What difference between throttleLatest and throttleLast in rxjava?

在Observable中,有兩種方法叫做throttleLast和throttleLatest。

大理石圖是相似的,但兩個內部代碼是不同的。

public final Observable<T> throttleLast(long intervalDuration, TimeUnit unit) {
    return sample(intervalDuration, unit);
}


public final Observable<T> sample(long period, TimeUnit unit, Scheduler scheduler, boolean emitLast) {
    ObjectHelper.requireNonNull(unit, "unit is null");
    ObjectHelper.requireNonNull(scheduler, "scheduler is null");
    return RxJavaPlugins.onAssembly(new ObservableSampleTimed<T>(this, period, unit, scheduler, emitLast));
}
public final Observable<T> throttleLatest(long timeout, TimeUnit unit, Scheduler scheduler, boolean emitLast) {
    ObjectHelper.requireNonNull(unit, "unit is null");
    ObjectHelper.requireNonNull(scheduler, "scheduler is null");
    return RxJavaPlugins.onAssembly(new ObservableThrottleLatest<T>(this, timeout, unit, scheduler, emitLast));
}

他們之間有什么區別?

查看@Slaw和@akarnokd的評論。

不是正面的,但是throttleLast可能會按固定的時間間隔運行,而且每當項目到達時,throttleLatest 都會重置超時 換句話說, 固定速率和固定延遲之間的差異。

它位於javadoc:throttleLatest“如果在此超時階段沒有從上游發出任何項目,則立即發出下一個上游項目,並從那時開始超時窗口。” ThrottleLast以固定的速率發出,如果沒有項目,則不發出任何內容。

我不太懂,所以我試着比較一下自己。

示例代碼

樣品

它們非常相似。 他們都有一個固定的時間窗口(上面的評論是錯誤的)。

唯一的區別是如何處理第一個項目。

  • ThrottleLast不會立即發出第一個項目,它將始終等待時間窗口傳遞,然后從該時間窗口發出最后一個值(如果有的話)
  • ThrottleLatest將立即發出第一個項目,之后它將與ThrottleLast表現相同。

暫無
暫無

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

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