gpt4 book ai didi

Swift Combine 的 CombineLatest 不会响应对其发布者之一的更新而触发

转载 作者:行者123 更新时间:2023-12-05 01:37:36 26 4
gpt4 key购买 nike

我正在合并两个发布者以确定 map View 的中心坐标应该是什么。这两家出版商是:

  1. 用户的初始位置由 CLLocationManager 确定(CLLocationManager 开始发送位置更新后报告的第一个位置)。
  2. 如果点击“在当前位置居中 map ”按钮,则为用户的当前位置。

在代码中:

    class LocationManager: NSObject, ObservableObject {

// The first location reported by the CLLocationManager.
@Published var initialUserCoordinate: CLLocationCoordinate2D?
// The latest location reported by the CLLocationManager.
@Published var currentUserCoordinate: CLLocationCoordinate2D?
// What the current map view center should be.
@Published var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 42.35843, longitude: -71.05977) // Default coordinate.

// A subject whose `send(_:)` method is being called elsewhere every time the user presses a button to center the map on the user's location.
var centerButtonTappedPublisher: PassthroughSubject<Bool, Never> = PassthroughSubject<Bool, Never>()

// The combined publisher that is where all my troubles lie.
var coordinatePublisher: AnyPublisher<CLLocationCoordinate2D, Never> {
Publishers.CombineLatest($initialUserCoordinate, centerButtonTappedPublisher)
.map { initialCoord, centerButtonTapped in
var latestCoord = initialCoord
if centerButtonTapped {
latestCoord = self.currentUserCoordinate
}
return latestCoord
}
.replaceNil(with: CLLocationCoordinate2D(latitude: 42.35843, longitude: -71.05977))
.eraseToAnyPublisher()
}

private var cancellableSet: Set<AnyCancellable> = []

//... Other irrelevant properties

private override init() {
super.init()

coordinatePublisher
.receive(on: RunLoop.main)
.assign(to: \.coordinate, on: self)
.store(in: &cancellableSet)

//... CLLocationManager set-up
}
}

extension LocationManager: CLLocationManagerDelegate {

//...

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// We are only interested in the user's most recent location.
guard let location = locations.last else { return }
let latestCoord = location.coordinate
if initialUserCoordinate == nil {
initialUserCoordinate = latestCoord
}
currentUserCoordinate = latestCoord
}

//...

}

$initialUserCoordinatecenterButtonTappedPublisher 这两个发布者都发布了更新 - 我已经确认了这一点。但是,合并的发布者 coordinatePublisher 仅在点击“当前位置的中心 map ”按钮时触发。当首次设置 initialUserCoordinate 属性时,它永远不会触发。

This question建议在 Publishers.CombineLatest($initialUserCoordinate, centerButtonTappedPublisher) 之后添加一个 .receive(on: RunLoop.main) 但这对我不起作用。

我做错了什么?

最佳答案

您需要使用 Publishers.Merge而不是 CombineLatest ,请参阅文档:

对于 Publishers.CombineLatest:

A publisher that receives and combines the latest elements from two publishers.

对于 Publishers.Merge

A publisher that emits an event when either upstream publisher emits an event.

关于Swift Combine 的 CombineLatest 不会响应对其发布者之一的更新而触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60844286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com