實例方法
throttle(for:
throttle(for:scheduler:latest:)
發布上遊發布者在指定時間間隔內發布的最新或第一個元素。
聲明
func throttle<S>(
for interval: S.SchedulerTimeType.Stride,
scheduler: S,
latest: Bool
) -> Publishers.Throttle<Self, S> where S : Scheduler
返回值
發出在指定時間間隔內收到的最新或第一個元素的發布者。
參數
interval
查找和發出最新或第一個元素的時間間隔,以調度程序的時間係統表示。
scheduler
在其上發布元素的調度程序。
latest
一個布爾值,指示是否發布最新元素。如果
false
,則發布者發出在間隔期間收到的第一個元素。
詳述
使用 Publisher/throttle(for:scheduler:latest:)
在您指定的時間間隔內有選擇地從上遊發布者重新發布元素。在節流間隔內從上遊接收的其他元素不會重新發布。
在下麵的示例中,
在 one-second 間隔上生成元素; Timer.TimerPublisher
Publisher/throttle(for:scheduler:latest:)
運算符傳遞第一個事件,然後在以下十秒間隔內僅重新發布最新事件:
cancellable = Timer.publish(every: 3.0, on: .main, in: .default)
.autoconnect()
.print("\(Date().description)")
.throttle(for: 10.0, scheduler: RunLoop.main, latest: true)
.sink(
receiveCompletion: { print ("Completion: \($0).") },
receiveValue: { print("Received Timestamp \($0).") }
)
// Prints:
// Publish at: 2020-03-19 18:26:54 +0000: receive value: (2020-03-19 18:26:57 +0000)
// Received Timestamp 2020-03-19 18:26:57 +0000.
// Publish at: 2020-03-19 18:26:54 +0000: receive value: (2020-03-19 18:27:00 +0000)
// Publish at: 2020-03-19 18:26:54 +0000: receive value: (2020-03-19 18:27:03 +0000)
// Publish at: 2020-03-19 18:26:54 +0000: receive value: (2020-03-19 18:27:06 +0000)
// Publish at: 2020-03-19 18:26:54 +0000: receive value: (2020-03-19 18:27:09 +0000)
// Received Timestamp 2020-03-19 18:27:09 +0000.
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相關用法
- Swift Result.Publisher tryLast(where:)用法及代碼示例
- Swift Result.Publisher tryCompactMap(_:)用法及代碼示例
- Swift Result.Publisher tryCatch(_:)用法及代碼示例
- Swift Result.Publisher timeout(_:scheduler:options:customError:)用法及代碼示例
- Swift Result.Publisher tryReduce(_:_:)用法及代碼示例
- Swift Result.Publisher tryFilter(_:)用法及代碼示例
- Swift Result.Publisher tryFirst(where:)用法及代碼示例
- Swift Result.Publisher tryPrefix(while:)用法及代碼示例
- Swift Result.Publisher tryScan(_:_:)用法及代碼示例
- Swift Result.Publisher tryMap(_:)用法及代碼示例
- Swift Result.Publisher tryDrop(while:)用法及代碼示例
- Swift Result.Publisher zip(_:_:_:)用法及代碼示例
- Swift Result.Publisher sink(receiveCompletion:receiveValue:)用法及代碼示例
- Swift Result.Publisher merge(with:_:_:)用法及代碼示例
- Swift Result.Publisher print(_:to:)用法及代碼示例
- Swift Result.Publisher sink(receiveValue:)用法及代碼示例
- Swift Result.Publisher eraseToAnyPublisher()用法及代碼示例
- Swift Result.Publisher setFailureType(to:)用法及代碼示例
- Swift Result.Publisher first(where:)用法及代碼示例
- Swift Result.Publisher output(at:)用法及代碼示例
- Swift Result.Publisher dropFirst(_:)用法及代碼示例
- Swift Result.Publisher merge(with:_:_:_:_:_:_:)用法及代碼示例
- Swift Result.Publisher flatMap(maxPublishers:_:)用法及代碼示例
- Swift Result.Publisher map(_:_:_:)用法及代碼示例
- Swift Result.Publisher catch(_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Result.Publisher throttle(for:scheduler:latest:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。