實例方法
handle
handleEvents(receiveSubscription:receiveOutput:receiveCompletion:receiveCancel:receiveRequest:)
當發布者事件發生時執行指定的閉包。
聲明
func handleEvents(
receiveSubscription: ((Subscription) -> Void)? = nil,
receiveOutput: ((Self.Output) -> Void)? = nil,
receiveCompletion: ((Subscribers.Completion<Self.Failure>) -> Void)? = nil,
receiveCancel: (() -> Void)? = nil,
receiveRequest: ((Subscribers.Demand) -> Void)? = nil
) -> Publishers.HandleEvents<Self>
返回值
當發布者事件發生時執行指定關閉的發布者。
參數
receiveSubscription
當發布者從上遊發布者收到訂閱時執行的可選閉包。此值默認為
nil
。receiveOutput
當發布者從上遊發布者接收到值時執行的可選閉包。此值默認為
nil
。receiveCompletion
當上遊發布者正常完成或因錯誤終止時執行的可選閉包。此值默認為
nil
。receiveCancel
當下遊接收者取消發布時執行的可選閉包。此值默認為
nil
。receiveRequest
當發布者收到更多元素的請求時執行的可選閉包。此值默認為
nil
。
詳述
采用Publisher/handleEvents(receiveSubscription:receiveOutput:receiveCompletion:receiveCancel:receiveRequest:)
當您想要在發布者生命周期的各個階段檢查元素時。
在下麵的示例中,整數發布者顯示了在element-processing 生命周期的每個階段打印調試信息的效果:
let integers = (0...2)
cancellable = integers.publisher
.handleEvents(receiveSubscription: { subs in
print("Subscription: \(subs.combineIdentifier)")
}, receiveOutput: { anInt in
print("in output handler, received \(anInt)")
}, receiveCompletion: { _ in
print("in completion handler")
}, receiveCancel: {
print("received cancel")
}, receiveRequest: { (demand) in
print("received demand: \(demand.description)")
})
.sink { _ in return }
// Prints:
// received demand: unlimited
// Subscription: 0x7f81284734c0
// in output handler, received 0
// in output handler, received 1
// in output handler, received 2
// in completion handler
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相關用法
- Swift Result.Publisher zip(_:_:_:)用法及代碼示例
- Swift Result.Publisher tryLast(where:)用法及代碼示例
- Swift Result.Publisher sink(receiveCompletion:receiveValue:)用法及代碼示例
- Swift Result.Publisher merge(with:_:_:)用法及代碼示例
- Swift Result.Publisher tryCompactMap(_:)用法及代碼示例
- 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 tryCatch(_:)用法及代碼示例
- Swift Result.Publisher dropFirst(_:)用法及代碼示例
- Swift Result.Publisher merge(with:_:_:_:_:_:_:)用法及代碼示例
- Swift Result.Publisher flatMap(maxPublishers:_:)用法及代碼示例
- Swift Result.Publisher map(_:_:_:)用法及代碼示例
- Swift Result.Publisher catch(_:)用法及代碼示例
- Swift Result.Publisher drop(while:)用法及代碼示例
- Swift Result.Publisher prefix(_:)用法及代碼示例
- Swift Result.Publisher timeout(_:scheduler:options:customError:)用法及代碼示例
- Swift Result.Publisher merge(with:_:)用法及代碼示例
- Swift Result.Publisher map(_:)用法及代碼示例
- Swift Result.Publisher append(_:)用法及代碼示例
- Swift Result.Publisher breakpointOnError()用法及代碼示例
- Swift Result.Publisher receive(on:options:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Result.Publisher handleEvents(receiveSubscription:receiveOutput:receiveCompletion:receiveCancel:receiveRequest:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。