实例方法
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:_:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。