Swift 使用flatMapLatest合并两个流

Swift 使用flatMapLatest合并两个流,swift,rx-swift,reactive-cocoa,reactive,Swift,Rx Swift,Reactive Cocoa,Reactive,在组合到flatMapLatest中的观察值时有问题 逻辑:在每个activity next事件上,我想将其与触发activityEvent后发生的下一个getCurrentLocation事件组合在一起,将它们组合在一个元组中,然后对其执行操作 现在是这样 ActivitiesController .start() .flatMapLatest { activity in LocationController.shared.getCurrentLocation

在组合到flatMapLatest中的观察值时有问题

逻辑:在每个activity next事件上,我想将其与触发activityEvent后发生的下一个
getCurrentLocation
事件组合在一起,将它们组合在一个元组中,然后对其执行操作

现在是这样

ActivitiesController
    .start()
    .flatMapLatest { activity in 
        LocationController.shared.getCurrentLocation().map { ($0, activity) }
    }
    .subscribe(onNext: { (activity, currentLocation in
        print("")
    })
    .disposed(by: disposeBag)
位置代码:

func getCurrentLocation() -> Observable<CLLocation> {
    self.requestLocationUseAuthorizationIfNotDetermined(for: .always)
    self.locationManager.requestLocation()
    return self.publishSubject.take(1) // take next object from the publish subject (only one)
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let location = locations.last, location.horizontalAccuracy > 0 else {
        return
    }
    self.publishSubject.onNext(location)
}
func getCurrentLocation()->可观察{
self.requestLocationUseAuthorizationIfNotDetermined(对于:。始终)
self.locationManager.requestLocation()
返回self.publishSubject.take(1)//从发布主题中获取下一个对象(仅一个)
}
func locationManager(manager:CLLocationManager,didUpdateLocations位置:[CLLocation]){
防护装置位置=位置。最后,位置。水平精度>0其他{
返回
}
self.publishSubject.onNext(位置)
}
因为我们知道
requestLocation()
触发了didUpdateLocations,所以我们认为逻辑应该可以工作,但它不能

结果是locationManager不总是更新和返回旧值,而不是新值


你们有什么想法吗?

你们需要使用
和latestfrom
而不是
flatMapLatest

LocationController.shared.getCurrentLocation().withLatestFrom(activityEvent) {
    // in here $0 will refer to the current location that was just emitted and
    // $1 will refer to the last activityEvent that was emitted.
    return ($0, $1)
}

尝试打印出位置数组并检查其内容。可能位置变化不足以被检测到。我不知道publishSubject.take()是如何工作的,但它似乎是在requestLocation完成之前返回的。这是你想要的行为吗?