實例方法
make
makeConnectable()
在發布者周圍創建一個可連接的包裝器。
聲明
func makeConnectable() -> Publishers.MakeConnectable<Self>
當
Failure
為 Never
時可用。返回值
包裝此發布者的ConnectablePublisher
。
詳述
在以下示例中,Publisher/makeConnectable()
使用 ConnectablePublisher
包裝其上遊發布者(Publishers/Share
的實例)。如果沒有這個,第一個接收器訂閱者將接收來自序列發布者的所有元素,並使其在第二個訂閱者附加之前完成。通過使發布者可連接,發布者在 ConnectablePublisher/connect()
調用之前不會生成任何元素。
let subject = Just<String>("Sent")
let pub = subject
.share()
.makeConnectable()
cancellable1 = pub.sink { print ("Stream 1 received: \($0)") }
// For example purposes, use DispatchQueue to add a second subscriber
// a second later, and then connect to the publisher a second after that.
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.cancellable2 = pub.sink { print ("Stream 2 received: \($0)") }
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.connectable = pub.connect()
}
// Prints:
// Stream 2 received: Sent
// Stream 1 received: Sent
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相關用法
- Swift Result.Publisher map(_:_:_:)用法及代碼示例
- Swift Result.Publisher map(_:)用法及代碼示例
- Swift Result.Publisher max()用法及代碼示例
- Swift Result.Publisher map(_:_:)用法及代碼示例
- Swift Result.Publisher mapError(_:)用法及代碼示例
- Swift Result.Publisher merge(with:_:_:)用法及代碼示例
- Swift Result.Publisher merge(with:_:_:_:_:_:_:)用法及代碼示例
- Swift Result.Publisher merge(with:_:)用法及代碼示例
- Swift Result.Publisher min()用法及代碼示例
- Swift Result.Publisher multicast(_:)用法及代碼示例
- Swift Result.Publisher merge(with:_:_:_:_:_:)用法及代碼示例
- Swift Result.Publisher multicast(subject:)用法及代碼示例
- Swift Result.Publisher merge(with:_:_:_:_:)用法及代碼示例
- Swift Result.Publisher merge(with:_:_:_:)用法及代碼示例
- Swift Result.Publisher measureInterval(using:options:)用法及代碼示例
- Swift Result.Publisher zip(_:_:_:)用法及代碼示例
- Swift Result.Publisher tryLast(where:)用法及代碼示例
- Swift Result.Publisher sink(receiveCompletion:receiveValue:)用法及代碼示例
- 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:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Result.Publisher makeConnectable()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。