实例方法
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 Result.Publisher first(where:)用法及代码示例
- Swift Result.Publisher filter(_:)用法及代码示例
- 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 output(at:)用法及代码示例
- Swift Result.Publisher tryCatch(_:)用法及代码示例
- Swift Result.Publisher dropFirst(_:)用法及代码示例
- Swift Result.Publisher merge(with:_:_:_:_:_:_:)用法及代码示例
- Swift Result.Publisher map(_:_:_:)用法及代码示例
- Swift Result.Publisher catch(_:)用法及代码示例
- Swift Result.Publisher drop(while:)用法及代码示例
- Swift Result.Publisher prefix(_:)用法及代码示例
- Swift Result.Publisher timeout(_:scheduler:options:customError:)用法及代码示例
- Swift Result.Publisher merge(with:_:)用法及代码示例
- Swift Result.Publisher map(_:)用法及代码示例
- Swift Result.Publisher append(_:)用法及代码示例
- Swift Result.Publisher breakpointOnError()用法及代码示例
- Swift Result.Publisher handleEvents(receiveSubscription:receiveOutput:receiveCompletion:receiveCancel:receiveRequest:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Result.Publisher flatMap(maxPublishers:_:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。