jun
jun

Reputation: 59

Why use the responseWith method?

In the process of reading the RXAlamofire source code, there is a place that I don't understand very well.

Since this method is an observable object for creating a DataRequest, why call the responseWith method?

func request<R: RxAlamofireRequest>(_ createRequest: @escaping (SessionManager) throws -> R) -> Observable<R> {
        return Observable.create { observer -> Disposable in
            let request: R
            do {
                request = try createRequest(self.base)
                observer.on(.next(request))


                request.responseWith(completionHandler: { response in
                    if let error = response.error {
                        observer.on(.error(error))
                    } else {
                        observer.on(.completed)
                    }
                })

                if !self.base.startRequestsImmediately {
                    request.resume()
                }

                return Disposables.create {
                    request.cancel()
                }
            } catch {
                observer.on(.error(error))
                return Disposables.create()
            }
        }
    }

Upvotes: -1

Views: 63

Answers (1)

Dan Favano
Dan Favano

Reputation: 118

I believe the authors of RXAlamofire use this as their convention. If you look at there request implementation All of the request methods return the result of a method responseXYZ. The response methods typically execute the request and respond with something (JSON, String, etc.) Sounds a bit confusing but its kind of like this request some data respond with something.

Upvotes: 0

Related Questions