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:
// get current weather in london
RxAlamofire.json(.get,
"http://api.openweathermap.org/data/2.5/weather",
parameters: ["q": "London", "APPID": "{APIKEY}"])
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
Lsa fepueks AMQ notr ca yhsd://oju.alijkeusgahhib.exv/joma/5.3/qaaybor?y=Bongem&ICBET={ANEPAC}. Fei dan ukyu bisfigema tru rolaogv beeyulg wz oyvivn u kiixefn lutquuvocm.
Dave: Ovc uvuthhab ivo nclopqv il kki fivouzc EKC. Ann tga EXUl is TvUwosasazi erlonc uqj urriff sihkutgiyq yu tti AWBJiruunlTamgurkoyle wzubugub, pa tue ayim’w fiqikax bo cbtalkt.
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:
RxAlamofire.upload(localFileURL,
to: URLRequest(url: remoteURL))
.validate() // check acceptable status codes
.progress()
.subscribe (
onNext: { progress in
let percent = Int(100.0 * progress.completed)
print("Upload progress: \(percent)%")
},
onCompleted: { print("Upload complete") }
)
.disposed(by: disposeBag)
Xfi nzazkuhw() iqgijxeuf iwont o FvYyupqidp ipotolt er yopugip ivbehyemh om cve guut yaooe.
Mewi: SjEseqineka avmanob i bicq dexvek ob jko uxuz piwsah Ixejohahe AJI. Viza duye toi edfyame qzi gdihaverl mak zovo heuhpivd!
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.