Intermediate Combine

Apr 13 2021 · Swift 5.3, macOS 11.1, Xcode 12.2

Part 1: Intermediate Combine

01. Networking with Combine

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Next episode: 02. Sharing Resources

Notes: 01. Networking with Combine

Prerequisites: Intermediate Swift knowledge, basic Combine knowledge, knowledge of URLSession.

Transcript: 01. Networking with Combine URLSession 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.