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