實例方法
flat
flatMap(maxPublishers:_:)
將上遊發布者的所有元素轉換為新發布者,最多可指定您指定的最大數量的發布者。
聲明
func flatMap<T, P>(
maxPublishers: Subscribers.Demand = .unlimited,
_ transform: @escaping (Self.Output) -> P
) -> Publishers.FlatMap<P, Self> where T == P.Output, P : Publisher, Self.Failure == P.Failure
返回值
將元素從上遊發布者轉換為該元素類型的發布者的發布者。
參數
maxPublishers
指定並發發布者訂閱的最大數量,如果未指定,則指定
Combine/Subscribers/Demand/unlimited
。transform
將元素作為參數並返回生成該類型元素的發布者的閉包。
詳述
Combine 的 flatMap(maxPublishers:_:)
運算符執行與 Swift 標準庫中的
運算符類似的函數,但將元素從一種發布者轉換為發送給訂閱者的新發布者。當您想根據收到的值為下遊訂閱者創建一係列新事件時,請使用 flatMap(_:)
flatMap(maxPublishers:_:)
。閉包根據接收到的值創建新的Publisher
。新的Publisher
可以發出多個事件,並且成功完成新的Publisher
不會完成整個流。新的Publisher
失敗會導致整個流失敗。
在下麵的示例中,PassthroughSubject
發布 WeatherStation
元素。 flatMap(maxPublishers:_:)
接收每個元素,從中創建一個
,並生成一個新的 URL
,它將發布從該 URLSession.DataTaskPublisher
加載的數據。URL
public struct WeatherStation {
public let stationID: String
}
var weatherPublisher = PassthroughSubject<WeatherStation, URLError>()
cancellable = weatherPublisher.flatMap { station -> URLSession.DataTaskPublisher in
let url = URL(string:"https://weatherapi.example.com/stations/\(station.stationID)/observations/latest")!
return URLSession.shared.dataTaskPublisher(for: url)
}
.sink(
receiveCompletion: { completion in
// Handle publisher completion (normal or error).
},
receiveValue: {
// Process the received data.
}
)
weatherPublisher.send(WeatherStation(stationID: "KSFO")) // San Francisco, CA
weatherPublisher.send(WeatherStation(stationID: "EGLC")) // London, UK
weatherPublisher.send(WeatherStation(stationID: "ZBBB")) // Beijing, CN
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相關用法
- 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 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:)用法及代碼示例
- Swift Optional.Publisher assertNoFailure(_:file:line:)用法及代碼示例
- Swift Optional.Publisher collect(_:)用法及代碼示例
- Swift Optional.Publisher combineLatest(_:_:_:_:)用法及代碼示例
- 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:)用法及代碼示例
- Swift Optional.Publisher tryReduce(_:_:)用法及代碼示例
- Swift Optional.Publisher map(_:_:_:)用法及代碼示例
- Swift Optional.Publisher prepend(_:)用法及代碼示例
- Swift Optional.Publisher removeDuplicates()用法及代碼示例
- Swift Optional.Publisher merge(with:_:_:_:_:_:_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Optional.Publisher flatMap(maxPublishers:_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。