實例方法
combine
combineLatest(_:_:_:_:)
訂閱另外三個發布者,並在接收到任何發布者的輸出時調用閉包。
聲明
func combineLatest<P, Q, R, T>(
_ publisher1: P,
_ publisher2: Q,
_ publisher3: R,
_ transform: @escaping (Self.Output, P.Output, Q.Output, R.Output) -> T
) -> Publishers.Map<Publishers.CombineLatest4<Self, P, Q, R>, T> where P : Publisher, Q : Publisher, R : Publisher, Self.Failure == P.Failure, P.Failure == Q.Failure, Q.Failure == R.Failure
返回值
一個從這個發布者和其他三個發布者接收和組合元素的發布者。
參數
publisher1
與第一個發布者合並的第二個發布者。
publisher2
第三個發布者與第一個發布者合並。
publisher3
與第一個發布者合並的第四個發布者。
transform
從每個發布者接收最新值並返回要發布的新值的閉包。
詳述
當您需要組合當前和 3 個附加發布者並使用指定已發布元素的閉包轉換值以發布新元素時,請使用 Publisher/combineLatest(_:_:_:_:)
。
合並的發布者將所有請求傳遞給all
上遊發布者。但是,它仍然遵循僅向下遊發送請求量的demand-fulfilling 規則。如果需求不是 Subscribers/Demand/unlimited
,它會從上遊發布者那裏刪除值。它通過為每個上遊使用 1 的緩衝區大小來實現這一點,並在每個緩衝區中保存最新的值。
所有上遊發布者都需要完成此發布者才能完成。如果上遊發布者從不發布值,則此發布者永遠不會完成。
在下麵的示例中,由於 Publisher/combineLatest(_:_:_:_:)
接收四個發布者發布的最新值,將它們相乘,然後重新發布結果:
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) { firstValue, secondValue, thirdValue, fourthValue in
return firstValue * secondValue * thirdValue * fourthValue
}
.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: 36. // pub = 2, pub2 = 2, pub3 = 9, pub4 = 1
// Result: 54. // pub = 3, pub2 = 2, pub3 = 9, pub4 = 1
// Result: 324. // pub = 3, pub2 = 12, pub3 = 9, pub4 = 1
// Result: 1404. // pub = 13, pub2 = 12, pub3 = 9, pub4 = 1
// Result: 2964. // pub = 13, pub2 = 12, pub3 = 19, pub4 = 1
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相關用法
- Swift Result.Publisher combineLatest(_:_:_:)用法及代碼示例
- Swift Result.Publisher combineLatest(_:_:)用法及代碼示例
- Swift Result.Publisher combineLatest(_:)用法及代碼示例
- Swift Result.Publisher compactMap(_:)用法及代碼示例
- Swift Result.Publisher contains(_:)用法及代碼示例
- Swift Result.Publisher collect(_:options:)用法及代碼示例
- Swift Result.Publisher collect(_:)用法及代碼示例
- Swift Result.Publisher catch(_:)用法及代碼示例
- Swift Result.Publisher zip(_:_:_:)用法及代碼示例
- Swift Result.Publisher tryLast(where:)用法及代碼示例
- Swift Result.Publisher sink(receiveCompletion:receiveValue:)用法及代碼示例
- Swift Result.Publisher merge(with:_:_:)用法及代碼示例
- 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:)用法及代碼示例
- Swift Result.Publisher tryCatch(_:)用法及代碼示例
- Swift Result.Publisher dropFirst(_:)用法及代碼示例
- Swift Result.Publisher merge(with:_:_:_:_:_:_:)用法及代碼示例
- Swift Result.Publisher flatMap(maxPublishers:_:)用法及代碼示例
- Swift Result.Publisher map(_:_:_:)用法及代碼示例
- Swift Result.Publisher drop(while:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Result.Publisher combineLatest(_:_:_:_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。