Combine comes with 2 special operators, dataTaskPublisher(for:) and decode()
that help you fetch data from the network in your Combine pipeline, and decode that
data using Swift’s built in decoders such as JSONDecoder
This content was released on Apr 13 2021. The official support period is 6-months
from this date.
Combine comes with 2 special operators, dataTaskPublisher(for:) and decode()
that help you fetch data from the network in your Combine pipeline, and decode that
data using Swift’s built in decoders such as JSONDecoder
Cinema mode
Mark complete
Download course materials
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
Prerequisites: Intermediate Swift knowledge, basic Combine knowledge, knowledge of URLSession.
Transcript: 01. Networking with CombineURLSession supports a variety of operations, such as data transfer tasks to retrieve contents of a URL, download tasks to retrieve the contents of a URL and save it to a file, upload tasks to upload files and data to a URL, stream task to stream data between two parties, and WebSocket task to connect to WebSockets. Only the first operation, data transfer tasks, expose a Combine publisher taking either a URL or a URL request. Let's look at that in an example. Start an example block and make a URL by passing in a URL for a JSON file. And if it fails, use a guard statement to return. Create the Combine pipeline, making sure to keep the resulting subscription. Otherwise it will immediately be canceled and the request will never execute. On the URLSession.shared instance, call dataTask Publisher for, passing in the URL you made earlier. Add a sync to the Combine pipeline and use the more complex sync receive completion receive value version. So both errors and receive values can be sent to the console. This is very important because networks are prone to failure. The receive value in the sync is a tuple, containing the data object and the URL response from the server. This is very similar to the closure, used in normal URLSession use, but with a simpler publisher abstraction from Combine. Codeable is a very powerful protocol in Swift that provides an encoding and decoding mechanism you should definitely be familiar with. There are several types of encoders and decoders in Foundation, but one pair stands out when it comes to networking, JSONDecoder and JSONEncoder. Let's continue the URLSession and use JSONDecoder to decode the JSON you downloaded, and then see how we can improve on that with the decode operation from Combine. Create the same subscription as you did in the URLSession example, and add the dataTask Publisher for operator. Remember that dataTask Publisher emits a tuple, so you have to use map to grab only the data part of the tuple. Since JSONDecoder.decode can throw an error, you have to put the try keyword in front of it, which means that you must use tryMap and not just map. Complete the rest of the Combine pipeline with the same sync as before. This works, but Combine introduces some syntactical sugar to help get around that tryMap block. The decode type decoder operator takes in a decoder instead of a data object, instead inspecting that data from upstream. You still need to extract out the data part of the tuple, so you still need to use map. Replace the tryMap block with the map operator followed by the decode operator.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.