用法一
combineLatest(_:_:_:)
聲明
func combineLatest<P, Q, R>(
_ publisher1: P,
_ publisher2: Q,
_ publisher3: R
) -> Publishers.CombineLatest4<Self, P, Q, R> where P : Publisher, Q : Publisher, R : Publisher, Self.Failure == P.Failure, P.Failure == Q.Failure, Q.Failure == R.Failure
返回值
一個從這個發布者和其他三個發布者接收和組合元素的發布者。
參數
publisher1
與第一個發布者合並的第二個發布者。
publisher2
第三個發布者與第一個發布者合並。
publisher3
與第一個發布者合並的第四個發布者。
詳述
當您希望下遊訂閱者在其中任何一個發布值時從多個發布者接收最新元素的元組時,請使用 Publisher/combineLatest(_:_:_:)-48buc
。要組合來自多個發布者的元素,請改用Publisher/zip(_:_:_:)-16rcy
。要僅接收來自多個發布者而不是元組的最新元素,請使用 Publisher/merge(with:_:_:)
。
合並的發布者將所有請求傳遞給all
上遊發布者。但是,它仍然遵循僅向下遊發送請求量的demand-fulfilling 規則。如果需求不是 Subscribers/Demand/unlimited
,它會從上遊發布者那裏刪除值。它通過為每個上遊使用 1 的緩衝區大小來實現這一點,並在每個緩衝區中保存最新的值。
所有上遊發布者都需要完成此發布者才能完成。如果上遊發布者從不發布值,則此發布者永遠不會完成。
在下麵的示例中,Publisher/combineLatest(_:_:_:)-48buc
接收來自任何發布者的輸入,將來自每個發布者的最新值組合成一個元組並發布它:
let pub = PassthroughSubject<Int, Never>()
let pub2 = PassthroughSubject<Int, Never>()
let pub3 = PassthroughSubject<Int, Never>()
let pub4 = PassthroughSubject<Int, Never>()
cancellable = pub
.combineLatest(pub2, pub3, pub4)
.sink { print("Result: \($0).") }
pub.send(1)
pub.send(2)
pub2.send(2)
pub3.send(9)
pub4.send(1)
pub.send(3)
pub2.send(12)
pub.send(13)
pub3.send(19)
//
// Prints:
// Result: (2, 2, 9, 1).
// Result: (3, 2, 9, 1).
// Result: (3, 12, 9, 1).
// Result: (13, 12, 9, 1).
// Result: (13, 12, 19, 1).
如果組合集中的任何單個發布者因失敗而終止,則該發布者也會失敗。
可用版本
用法二
combineLatest(_:_:_:)
聲明
func combineLatest<P, Q, T>(
_ publisher1: P,
_ publisher2: Q,
_ transform: @escaping (Self.Output, P.Output, Q.Output) -> T
) -> Publishers.Map<Publishers.CombineLatest3<Self, P, Q>, T> where P : Publisher, Q : Publisher, Self.Failure == P.Failure, P.Failure == Q.Failure
返回值
一個從這個發布者和其他兩個發布者接收和組合元素的發布者。
參數
publisher1
與第一個發布者合並的第二個發布者。
publisher2
第三個發布者與第一個發布者合並。
transform
從每個發布者接收最新值並返回要發布的新值的閉包。
詳述
使用 combineLatest<P, Q>(_:,_:)
組合當前和兩個附加發布者,並使用您指定的閉包轉換它們以將新值發布到下遊。
合並的發布者將所有請求傳遞給all
上遊發布者。但是,它仍然遵循僅向下遊發送請求量的demand-fulfilling 規則。如果需求不是 .unlimited
,它會從上遊發布者那裏刪除值。它通過為每個上遊使用 1 的緩衝區大小來實現這一點,並在每個緩衝區中保存最新的值。所有上遊發布者都需要完成此發布者才能完成。如果上遊發布者從不發布值,則此發布者永遠不會完成。如果任何組合發布者因失敗而終止,則此發布者也會失敗。
在下麵的示例中,combineLatest()
接收三個發布者發布的最新值,將它們相乘,然後重新發布結果:
let pub = PassthroughSubject<Int, Never>()
let pub2 = PassthroughSubject<Int, Never>()
let pub3 = PassthroughSubject<Int, Never>()
cancellable = pub
.combineLatest(pub2, pub3) { firstValue, secondValue, thirdValue in
return firstValue * secondValue * thirdValue
}
.sink { print("Result: \($0).") }
pub.send(1)
pub.send(2)
pub2.send(2)
pub3.send(10)
pub.send(9)
pub3.send(4)
pub2.send(12)
// Prints:
// Result: 40. // pub = 2, pub2 = 2, pub3 = 10
// Result: 180. // pub = 9, pub2 = 2, pub3 = 10
// Result: 72. // pub = 9, pub2 = 2, pub3 = 4
// Result: 432. // pub = 9, pub2 = 12, pub3 = 4
可用版本
相關用法
- Swift Optional.Publisher combineLatest(_:_:_:_:)用法及代碼示例
- Swift Optional.Publisher combineLatest(_:_:)用法及代碼示例
- Swift Optional.Publisher combineLatest(_:)用法及代碼示例
- Swift Optional.Publisher compactMap(_:)用法及代碼示例
- Swift Optional.Publisher collect(_:)用法及代碼示例
- Swift Optional.Publisher contains(_:)用法及代碼示例
- Swift Optional.Publisher collect(_:options:)用法及代碼示例
- Swift Optional.Publisher catch(_:)用法及代碼示例
- 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 mapError(_:)用法及代碼示例
- Swift Optional.Publisher tryAllSatisfy(_:)用法及代碼示例
- Swift Optional.Publisher zip(_:_:_:)用法及代碼示例
- Swift Optional.Publisher sink(receiveValue:)用法及代碼示例
- Swift Optional.Publisher scan(_:_:)用法及代碼示例
- Swift Optional.Publisher throttle(for:scheduler:latest:)用法及代碼示例
- Swift Optional.Publisher assertNoFailure(_:file:line:)用法及代碼示例
- Swift Optional.Publisher share()用法及代碼示例
- Swift Optional.Publisher merge(with:_:_:)用法及代碼示例
- Swift Optional.Publisher map(_:_:)用法及代碼示例
- Swift Optional.Publisher assign(to:on:)用法及代碼示例
- Swift Optional.Publisher zip(_:_:_:_:)用法及代碼示例
- Swift Optional.Publisher output(in:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Optional.Publisher combineLatest(_:_:_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。