0
votes

I am trying to learn RxSwift. I had been using Moya and now want to try my hand at RxSwift. Totally confused.

 let popServiceProvider = MoyaProvider<PopRESTService>()
    let o1 = popServiceProvider.rx.request(.getPopByCity(city: "London")).asObservable()
    let o2 = popServiceProvider.rx.request(.getPopByCity(city: "Tokyo")).asObservable()

    let pObservables:[Observable<Response>] = [o1, o2]
    let stats: Observable<Response> = Observable.combineLatest(pObservables)
    let m = stats.map({ a in
        print(a)
    })  

First I get an error on let stats:

Cannot convert value of type 'Observable<[Response]>' to specified type 'Observable'

am I even going in the right direction?

EDIT:

Even this doesn't work. Never first the subscribe:

   wServiceProvider.rx.request(.getWeatherByCity(city: "Chicago"))
    .debug()
    .map(Weather.self)
    .subscribe({ (event) in
        switch event {
        case let .success(response):
            print(response)

        case let .error(response):
            print(response)
        }
    })
    .disposed(by: self.disposeBag)
2

2 Answers

0
votes

I recommend using Observable.combineLatest(o1, o2) variant of combineLatest (this form is limited by 8 arguments)

Assuming that type returned from case getPopByCity(city: String) might look like:

struct PopByCity: Decodable {
  let count: Float
}

you probably want to implement the request like that:

Observable.combineLatest(o1, o2)
  .map { (resp1: Response, resp2: Response) -> [PopByCity] in
    let pop1 = try? JSONDecoder().decode(PopByCity.self, from: resp1.data)
    let pop2 = try? JSONDecoder().decode(PopByCity.self, from: resp2.data)
    return [pop1, pop2].compactMap { $0 }
  }
  .subscribe(onNext: { (result: [PopByCity]) in
    print(result)
  })
  .disposed(by: disposeBag)

But if you still need an array of requests:

Observable.combineLatest([o1, o2])
  .map { (responses: [Response]) -> [PopByCity] in
    responses.map { try? JSONDecoder().decode(PopByCity.self, from: $0.data) }
      .compactMap { $0 }
  }
  .subscribe(onNext: { (result: [PopByCity]) in
    print(result)
  })
  .disposed(by: disposeBag)

For the second option, keep in mind, that it is not a good practice to send tons of async request at the same time. For many request need to be performed I recommend using Concat operator or combination of Concat + Reduce if you have to handle all responses in one place after the last response.

0
votes

My misunderstanding of Apple XCTest. I needed to add:

                expectation.fulfill()

in my subscribe.

Observable.combineLatest([o1, o2])
        .map { (responses: [Response]) -> [Weather] in
            responses.map { try? JSONDecoder().decode(Weather.self, from: $0.data) }
                .compactMap { $0 }
        }
        .debug()
        .subscribe(onNext: { (result: [Weather]) in
            print(result)
            expectation.fulfill()

        })
        .disposed(by: disposeBag)