0
votes

I am using Moya in my swift app for network requests.

I have used alamofire earlier and I am familiar with how to make post, get requests and read the response .

Following is the code where I am making a request and subscribing to the observable

provider.request(.getDetails)
            .mapArray(Post.self)
            .subscribe { event -> Void in
                switch event {
                case .next(let post):
                 self.sampleText.text = posts.first?.title
                case .error(let error):
                    print(error)
                default:
                    break
                }
            }.addDisposableTo(disposeBag)

In the .next case I also want to retrieve the status code and response.data.

I am able to do so when the observable is of type Response but when I map it to type Post I cannot get the status code.

How can I get the status code of the request in .next or error case

Any help will be appreciated. Thank you.

1

1 Answers

0
votes

Maybe this way? Using zip:

let provider: RxMoyaProvider<Api> = RxMoyaProvider<Api>()
let observable1: Observable<Response> = provider
    .request(.getDetails)
    .shareReplay(1)
let observable2: Observable<[Post]> = observable1
    .map({ _ in () }) // You map to Post.self here
    .shareReplay(1)
Observable
    .zip(observable1, observable2){($0, $1)}
    .subscribe({ (event) in
        // event has Response + Posts array
    })

also combineLatest can help in the same way as zip