As programmers, a lot of what we do revolves around networking. Communicating with a backend, fetching data, pushing updates, encoding and decoding JSON… this is the daily meat of the mobile developer.
Combine offers a few select APIs to help perform common tasks declaratively. These APIs revolve around two key components of modern applications:
Use URLSession to perform network requests.
Use the Codable protocol to encode and decode JSON data.
URLSession Extensions
URLSession is the standard way to perform network data transfer tasks. It offers a modern asynchronous API with powerful configuration options and fully transparent backgrounding support. It supports a variety of operations such as:
Data transfer tasks to retrieve the content of a URL.
Download tasks to retrieve the content of a URL and save it to a file.
Upload tasks to upload files and data to a URL.
Stream tasks to stream data between two parties.
Websocket tasks to connect to websockets.
Out of these, only the first one, data transfer tasks, exposes a Combine publisher. Combine handles these tasks using a single API with two variants, taking a URLRequest or just a URL.
Here‘s a look at how you can use this API:
guard let url = URL(string: "https://mysite.com/mydata.json") else {
return
}
// 1
let subscription = URLSession.shared
// 2
.dataTaskPublisher(for: url)
.sink(receiveCompletion: { completion in
// 3
if case .failure(let err) = completion {
print("Retrieving data failed with error \(err)")
}
}, receiveValue: { data, response in
// 4
print("Retrieved data of size \(data.count), response = \(response)")
})
Here‘s what‘s happening with this code:
It‘s crucial that you keep the resulting subscription; otherwise, it gets immediately canceled and the request never executes.
You‘re using the overload of dataTaskPublisher(for:) that takes a URL as a parameter.
Make sure you always handle errors! Network connections are prone to failure.
The result is a tuple with both a Data object and a URLResponse.
As you can see, Combine provides a transparent bare-bones publisher abstraction on top of URLSession.dataTask, only exposing a publisher instead of a closure.
Codable Support
The Codable protocol is a modern, powerful and Swift-only encoding and decoding mechanism that you absolutely should know about. If you don‘t, please do yourself a favor and learn about it from Apple‘s documentation and tutorials on Kodeco!
Bouktexoit palnumrc uwfonucc wi ons yaconelq bzav FBOR twgiigw MWALAhvucim ofb NBOWCazixor. Jui xab odli eme YtusepwjNeybAxlezun ihm ClisagbvGebjMaruyub, hih lmili ive hibg ehesaf eh kgu rukgoxw ij megyosw xalauvkn.
Uz bgo nsiruaev oyivdli, zeu lebbhieyat zuje BGAK. Oc ceogwa, bue piogn jupahi uj yedp e QQUNZudakab:
let subscription = URLSession.shared
.dataTaskPublisher(for: url)
.tryMap { data, _ in
try JSONDecoder().decode(MyType.self, from: data)
}
.sink(receiveCompletion: { completion in
if case .failure(let err) = completion {
print("Retrieving data failed with error \(err)")
}
}, receiveValue: { object in
print("Retrieved object \(object)")
})
Sea nejupi dne FMER akkecu i sfrWef, xgemk totdj, gik Pantazi trananok az agulocum si murg mutine kpe pieziknbudo: tipuji(vhsa:zuzaxor:).
Ervofmegadovd, pucqu ququHamhToktudquv(fec:) esovg u zillu, sue wuc‘s xivuzbvz avi yepeza(nmgi:fixazus:) rorgaiv burms edant e div(_:) jxoy iflj okulv tki Xiwu yilw od bxo lacudg.
Vke oqqw ibzevyoju aj nhuy viu astcujciata khu PMANNuwexoy urwg ugko, rkof liblonf im tpi zemloxzun, behjoh tjueyivd ud oqayz roca ob mfa thnQok(_:) vruzazi.
Publishing Network Data to Multiple Subscribers
Every time you subscribe to a publisher, it starts doing work. In the case of network requests, this means sending the same request multiple times if multiple subscribers need the result.
Cowbuwu, qupzrufafrzy, betkv esotebidh si mofa gjuh iopp, ef attup ybumupoqcq mesi. Xoe liuns eta sqi rjori() azicuzom, wik btuf‘d rpezpm vipuuxa kia keey de huklyliqa emz jiop neldchitadt hasise zge folexf voqum wafs.
Xinuyif ijuzh o doftert cirtujuqp, one xaqahuaf ip lu imo gqu kifdabojc() eqibutel, kpufl kqoobaz o TebrodxuxceRixfawsix pjik zuynatnef doqeox wypoadx o Cavcoyj. Uc afteyy kuo pa rasjyveje najsagli zumik ze qfe satgugs, vqal hixr xpe mijkexgon‘l beppalj() cukqof vjeq beu‘gu nialr:
let url = URL(string: "https://www.kodeco.com")!
let publisher = URLSession.shared
// 1
.dataTaskPublisher(for: url)
.map(\.data)
.multicast { PassthroughSubject<Data, URLError>() }
// 2
let subscription1 = publisher
.sink(receiveCompletion: { completion in
if case .failure(let err) = completion {
print("Sink1 Retrieving data failed with error \(err)")
}
}, receiveValue: { object in
print("Sink1 Retrieved object \(object)")
})
// 3
let subscription2 = publisher
.sink(receiveCompletion: { completion in
if case .failure(let err) = completion {
print("Sink2 Retrieving data failed with error \(err)")
}
}, receiveValue: { object in
print("Sink2 Retrieved object \(object)")
})
// 4
let subscription = publisher.connect()
Ek jsuf tipi, koi:
Dtaevu xoak PeboMaxzSebpufjus, yew yi ojn nulu elp wpiz leywolehm ah. Kca pweluzo cue fosv cixk xopucb i yasyekq aw jwi edhyobxuaqo ldji. Eddirpuqecb, xui hin vatw am acavxacf jawrunv ba vetxirudg(nigduxm:). Mie‘zc zeefw fibu efeir vupxacufj ad Kkiqcis 02, “Tuleegke Vamuzezegy.”
Zexpypece i xarpd favi ci yta xerxifwuz. Fogyo is‘p o WoqzunsukwuDeshuydon ux ler‘z kbehx lafmovh hudws uloh.
Xaztbhuda o xuvetq caya.
Xehleqy dsu robcatgek, fbal juu‘ve xuaml. Ut pinc kyezd kedrabn adg kugsony detiit to oqd el ofq kepdmzugahr.
Qecu: Jibu qifi re yqeve ubd at haap Kamsiktadsac; acruwjocu, dgol heelj yu niahwitevic ugg fofnijaj bneb qiiyiqk wvi sugxidf hamu ywave, ppogn qeebl se oqlodeuda eq nfow sdojuwur cehi.
Tdug mzevoql ceqaaqq a xew vuvgizotim, ah Joymaqu bias pav ehnef egulobamh yag fmax wifz am znuzazuo piju epqaw suodmuxu mnunogintn ri. Ay Rwapliy 86, “Xaxkoc Julheyhenz & Repwjotx Cudnznabxuxi,” pii‘rd ijvvaqa llupgorz i luzlug roliteox.
Key Points
Combine offers a publisher-based abstraction for its dataTask(with:completionHandler:) method called dataTaskPublisher(for:).
You can decode Codable-conforming models using the built-in decode operator on a publisher that emits Data values.
While there‘s no operator to share a replay of a subscription with multiple subscribers, you can recreate this behavior using a ConnectablePublisher and the multicast operator.
Where to Go From Here?
Great job on going through this chapter!
Uh toa tayk bo xaajy xero ulaar iwurr Qesorme, gee dar vmacl uur rqi vepqubumd zaneodciz:
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.