实例方法
merge(with:
merge(with:_:_:_:_:_:)
将来自该发布者的元素与来自其他六个发布者的元素组合在一起,提供交错的元素序列。
声明
func merge<B, C, D, E, F, G>(
with b: B,
_ c: C,
_ d: D,
_ e: E,
_ f: F,
_ g: G
) -> Publishers.Merge7<Self, B, C, D, E, F, G> where B : Publisher, C : Publisher, D : Publisher, E : Publisher, F : Publisher, G : 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, D.Failure == E.Failure, D.Output == E.Output, E.Failure == F.Failure, E.Output == F.Output, F.Failure == G.Failure, F.Output == G.Output
返回值
当任何上游发布者发出事件时发出事件的发布者。
参数
b
第二个发布者。
c
第三个发布者。
d
第四发布者。
e
第五发布者。
f
第六位发布者。
g
第七位发布者。
详述
当您想要在任何上游发布者发出元素时接收新元素时,请使用 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>()
let pubE = PassthroughSubject<Int, Never>()
let pubF = PassthroughSubject<Int, Never>()
let pubG = PassthroughSubject<Int, Never>()
cancellable = pubA
.merge(with: pubB, pubC, pubD, pubE, pubE, pubG)
.sink { print("\($0)", terminator: " " ) }
pubA.send(1)
pubB.send(40)
pubC.send(90)
pubD.send(-1)
pubE.send(33)
pubF.send(44)
pubG.send(54)
pubA.send(2)
pubB.send(50)
pubC.send(100)
pubD.send(-2)
pubE.send(33)
pubF.send(33)
pubG.send(54)
//Prints: "1 40 90 -1 33 44 54 2 50 100 -2 33 33 54"
合并的发布者继续发出元素,直到所有上游发布者完成。如果上游发布者产生错误,则合并的发布者会因该错误而失败。
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相关用法
- Swift Result.Publisher merge(with:_:_:_:_:_:_:)用法及代码示例
- Swift Result.Publisher merge(with:_:_:_:_:)用法及代码示例
- Swift Result.Publisher merge(with:_:_:_:)用法及代码示例
- Swift Result.Publisher merge(with:_:_:)用法及代码示例
- Swift Result.Publisher merge(with:_:)用法及代码示例
- Swift Result.Publisher measureInterval(using:options:)用法及代码示例
- Swift Result.Publisher map(_:_:_:)用法及代码示例
- Swift Result.Publisher map(_:)用法及代码示例
- Swift Result.Publisher min()用法及代码示例
- Swift Result.Publisher multicast(_:)用法及代码示例
- Swift Result.Publisher multicast(subject:)用法及代码示例
- Swift Result.Publisher max()用法及代码示例
- Swift Result.Publisher map(_:_:)用法及代码示例
- Swift Result.Publisher mapError(_:)用法及代码示例
- Swift Result.Publisher makeConnectable()用法及代码示例
- 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 merge(with:_:_:_:_:_:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。