Now Loading

Wait multiple observable requests to finish using RXSwift

DWQA QuestionsCategory: QuestionsWait multiple observable requests to finish using RXSwift
Kenneth Williams Staff asked 3 years ago

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 {
                } else {
                    guard let leg = routes.first?.legs?.first else {

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

                    let viewModel = GasStationTableCellViewModel(station: station)

            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):