【问题标题】:swift combine flatmap sequential operationswift combine flatmap 顺序操作
【发布时间】:2020-09-25 17:38:48
【问题描述】:
extension CLGeocoder {
    func reverseGeocodeLocationPublisher(_ location: CLLocation, preferredLocale locale: Locale? = nil) -> AnyPublisher<CLPlacemark, Error> {
        Future<CLPlacemark, Error> { promise in
            self.reverseGeocodeLocation(location, preferredLocale: locale) { placemarks, error in
                guard let placemark = placemarks?.first else {
                    return promise(.failure(error ?? CLError(.geocodeFoundNoResult)))
                }
                return promise(.success(placemark))
            }
        }.eraseToAnyPublisher()
    }
}

$stations.flatMap(maxPublishers: .max(1)) { (station) -> AnyPublisher<CLPlacemark, Error> in
            let location = CLLocation(latitude: station.latitude, longitude: station.longitude)
            self.geocoder.reverseGeocodeLocationPublisher(location)

        }.eraseToAnyPublisher().sink(receiveCompletion: { completion in
            print("done")
        }, receiveValue: { placemark in
            print("placemark:", placemark)
        }).store(in: &cancellableSet)

错误: Instance method 'flatMap(maxPublishers:_:)' requires the types 'Published&lt;[Station]&gt;.Publisher.Failure' (aka 'Never') and 'Error' be equivalent

我想要实现的是我有一个包含location 的电台列表,我想依次reverseGeocodeLocation

【问题讨论】:

  • 您真正需要做的就是阅读错误信息。 :)

标签: swift combine flatmap


【解决方案1】:

您看到的错误是由于Publishers.FlatMap 要求上游和新发布者的Failure 相同。 FlatMap 返回一个 FailureError 类型,但 $stations 有一个 FailureNever,所以你需要调整它:

.setFailureType(to: Error.self)

由于stations 是一个数组,并且您希望按顺序处理每个元素,因此您需要使用Publishers.Sequence 发布者(可通过Array.publisher 获得),它将逐个发出每个元素:

stations.publisher

把这两件事放在一起:

$stations.flatMap { stations in
            stations.publisher
         }
         .setFailureType(to: Error.self)
         .flatMap { station in 
            let location = CLLocation(latitude: station.latitude, ...)
            return self.geocoder.reverseGeocodeLocationPublisher(location)
         }

【讨论】:

  • 这个编译但不起作用filteredStations.publisher.setFailureType(to: Error.self).flatMap(maxPublishers: .max(1)) { (station) -&gt; AnyPublisher&lt;CLPlacemark, Error&gt; in let location = CLLocation(latitude: station.latitude, longitude: station.longitude) return self.geocoder.reverseGeocodeLocationPublisher(location) }.eraseToAnyPublisher().sink(receiveCompletion: { completion in print("done") }, receiveValue: { placemark in print("placemark:", placemark) }).store(in: &amp;cancellableSet)
  • 这不会编译$filteredStations.setFailureType(to: Error.self).flatMap(maxPublishers: .max(1)) { (station) -&gt; AnyPublisher&lt;CLPlacemark, Error&gt; in let location = CLLocation(latitude: station.latitude, longitude: station.longitude) return self.geocoder.reverseGeocodeLocationPublisher(location) }.eraseToAnyPublisher().sink(receiveCompletion: { completion in print("done") }, receiveValue: { placemark in print("placemark:", placemark) }).store(in: &amp;cancellableSet)
  • 错误是 'Publishers.SetFailureType.Publisher, Error>.Output' (aka 'Array') 类型的值没有成员 'latitude'跨度>
  • @user3165940。这看起来像一个不相关的错误,不是吗? filteredStations 是什么?
  • 它的这个@Published var filteredStations = [Station]()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-27
  • 2016-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多