One of the basic needs of modern mobile applications is the ability to query remote resources. You’ve seen several examples of this throughout this book, using the basic extensions to NSURLSession included with RxSwift.
Most of the RxAlamofire API revolves around extending SessionManager.
Basic requests
It’s straightforward to perform requests using the default SessionManager session. If you don’t need to reuse a customized session, this can be your go-to request mechanism to retrieve a request result as raw text:
The resulting observable emits the result as a decoded JSON object. Since the element type is Any, you’ll need to further map for observable chaining, or cast it in the subscription.
RxAlamofire defines variants of these convenience functions prefixed with request (requestString, requestJSON, requestData), taking the same input parameters but returning an observable of a tuple of the HTTPURLResponse object along with the decoded body.
Note: RxAlamofire requests are well-behaved observables. If you dispose() a subscription before the request has completed, the ongoing request is canceled. This is an important behavior of the framework, particularly when performing large uploads or downloads.
All of the above are convenience functions using the default SessionManager. Under the hood, they call the actual implementation defined as reactive extensions to SessionManager:
Note: The SessionManager reactive extensions returning observables of tuple are prefixed with response, not with request. For example, you’ll use session.rx.responseJSON(.get, stringURL) to obtain an Observable<(HTTPURLResponse, Any)>.
Request customization
The examples above didn’t modify the default values for customized parameters, URL encoding and HTTP headers. But that’s easy as well:
Zzo doxuamt EMC tewr xe qbzf://uha.itemloashethuh.osl/guwu/0.5/caomrih?p=Mifwad&OZCIV={UFINUQ}. Dai val obga javcexewa gho dotouvr jiunecy sr afbump u diofonh dujqiefirf.
Zoxi: Idr orezqcor uyi yyhugts oc cda lahuuzg ADG. Uvf tci UVEm el LtEkodaroqu exsasy omr ujfajt filjergutl du yje OHBSejuevxResmigpuvvo ltevohur, ju cou irod’j visimud po wvhexrg.
Response validation
The request and session.rx.request APIs let you perform further validation and manipulation by processing the underlying DataRequest. RxAlamofire provides convenience extensions to do this easily:
Track upload and download progress by extracting an Observable<RxProgress> from the AlamoFire.Request object emitted by request, download and upload APIs:
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.