熱門發問分類: Orangebox 全新系統Wait multiple observable requests to finish using RXSwift
管理員 提問於 5 年 前發佈

have a list of observables that are requests for google distance and duration info from an specific point. I’m trying to load my screen only when all this information is fetched, but my subscribe on next for those observables are never called (the line “observer.onNext(viewModel)” is called and has the information already fetched, only the subscribe(onNext) is not being called). How can I wait til those observables complete?


func stationInfoObservable(userLocation: CLLocationCoordinate2D, stations: [Station]) -> [Observable] {

    var observables: [Observable] = []

    for station in stations {
        observables.append(Observable.create({ observer in
            guard let toCoordinate = station.coordinate() else { return Disposables.create() }

            self.mapDirections.routes(from: userLocation.asPlace(), to: toCoordinate.asPlace()) { routes, error in
                if let error = error {
                    logger.error(error)
                } else {
                    guard let leg = routes.first?.legs?.first else {
                        return
                    }

                    guard let distance = leg.distance?.text, let duration = leg.duration?.text else { return }
                    station.distanceInKMFromUserLocation = distance
                    station.distanceInMinutesFromUserLocation = duration

                    let viewModel = GasStationTableCellViewModel(station: station)

                    observer.onNext(viewModel)
                    observer.onCompleted()
                }
            }
            return Disposables.create()
        }))
    }

    return observables
}

I’m trying to subscribe this way (EDIT: I’m now trying to use zip, but the the drive / subscribe continues not being called):

  • Share: