实例方法
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:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。