实例方法
merge(with:
merge(with:_:_:)
将来自该发布者的元素与来自其他三个发布者的元素组合在一起,提供交错的元素序列。
声明
func merge<B, C, D>(
with b: B,
_ c: C,
_ d: D
) -> Publishers.Merge4<Self, B, C, D> where B : Publisher, C : Publisher, D : Publisher, Self.Failure == B.Failure, Self.Output == B.Output, B.Failure == C.Failure, B.Output == C.Output, C.Failure == D.Failure, C.Output == D.Output
返回值
当任何上游发布者发出事件时发出事件的发布者。
参数
b
第二个发布者。
c
第三个发布者。
d
第四发布者。
详述
当您想要在任何上游发布者发出元素时接收新元素时,请使用 Publisher/merge(with:_:_:)
。要在所有上游发布者发出值时从所有上游发布者那里接收最新值的元组,请使用 Publisher/combineLatest(_:_:_:)-48buc
。要组合来自多个上游发布者的元素,请使用 Publisher/zip(_:_:_:)-16rcy
。
在此示例中,当 Publisher/merge(with:_:_:)
从上游发布者接收输入时,它会将交错的元素重新发布到下游:
let pubA = PassthroughSubject<Int, Never>()
let pubB = PassthroughSubject<Int, Never>()
let pubC = PassthroughSubject<Int, Never>()
let pubD = PassthroughSubject<Int, Never>()
cancellable = pubA
.merge(with: pubB, pubC, pubD)
.sink { print("\($0)", terminator: " " )}
pubA.send(1)
pubB.send(40)
pubC.send(90)
pubD.send(-1)
pubA.send(2)
pubB.send(50)
pubC.send(100)
pubD.send(-2)
// Prints: "1 40 90 -1 2 50 100 -2 "
合并的发布者继续发出元素,直到所有上游发布者完成。如果上游发布者产生错误,则合并的发布者会因该错误而失败。
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相关用法
- Swift Optional.Publisher merge(with:_:_:_:_:_:_:)用法及代码示例
- Swift Optional.Publisher merge(with:_:_:_:)用法及代码示例
- Swift Optional.Publisher merge(with:_:_:_:_:)用法及代码示例
- Swift Optional.Publisher merge(with:_:_:_:_:_:)用法及代码示例
- Swift Optional.Publisher merge(with:_:)用法及代码示例
- Swift Optional.Publisher measureInterval(using:options:)用法及代码示例
- Swift Optional.Publisher mapError(_:)用法及代码示例
- Swift Optional.Publisher map(_:_:)用法及代码示例
- Swift Optional.Publisher map(_:_:_:)用法及代码示例
- Swift Optional.Publisher multicast(subject:)用法及代码示例
- Swift Optional.Publisher max()用法及代码示例
- Swift Optional.Publisher map(_:)用法及代码示例
- Swift Optional.Publisher min()用法及代码示例
- Swift Optional.Publisher multicast(_:)用法及代码示例
- Swift Optional.Publisher makeConnectable()用法及代码示例
- Swift Optional.Publisher reduce(_:_:)用法及代码示例
- Swift Optional.Publisher tryDrop(while:)用法及代码示例
- Swift Optional.Publisher debounce(for:scheduler:options:)用法及代码示例
- Swift Optional.Publisher breakpoint(receiveSubscription:receiveOutput:receiveCompletion:)用法及代码示例
- Swift Optional.Publisher catch(_:)用法及代码示例
- Swift Optional.Publisher tryAllSatisfy(_:)用法及代码示例
- Swift Optional.Publisher zip(_:_:_:)用法及代码示例
- Swift Optional.Publisher sink(receiveValue:)用法及代码示例
- Swift Optional.Publisher scan(_:_:)用法及代码示例
- Swift Optional.Publisher throttle(for:scheduler:latest:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Optional.Publisher merge(with:_:_:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。