Integrate Combine Into an App

Aug 5 2021 · Swift 5.4, macOS 11.3, Xcode 12.5

Part 1: Define a View Model

03. Create Data Publishers

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 02. Use @Published to Publish State Next episode: 04. Use Publishers in the ViewModel

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Before we can use publishers in our View Model, we have to define them first. We need to use them in our production and test code. So some overarching protocols will work best here. Then we'll need publishers that will generate jokes for both production and test, as well as their translations again, in production and test. Let's go to the code and define some protocols and implement the necessary services. Go to the Protocols folder and select the JokeServiceDataPublisher.swift file, and you'll see an empty JokeServiceDataPublisher protocol. Add a publisher function that returns an AnyPublisher that takes in Data and returns a URL Error. In the same folder, go to the TranslationServiceDataPublisher.swift file and add a publisher function to this protocol... (typing) This publisher looks similar in form to the JokeServiceDataPublisher publisher, but takes on a Joke and a String representing the LanguageCode. The return types for these two functions indicate we'll be using erase to AnyPublisher at the end of the combined pipeline we'll build. Speaking of which, go to the Services folder and select the TranslationService.swift file, and add an extension so TranslationService will adopt TranslationServiceDataPublisher... In this function, a DataTaskPublisher is formed from the Joke and LanguageCode using a helper method found earlier in the file. The map operator is used to grab the Data portion of the return Tupelo and erase to AnyPublisher is used to wrap the publisher in an instance of AnyPublisher, which hides the true nature of the underlying publisher. Do something similar inside the services folder for JokeService.swift. This publisher uses the private property of the JokeService to get a random Joke from the JokeService URL. Map is again used to grab the Data portion of the Tupelo and erase AnyPublisher performs the type ratio force. Now to the testing code, the starter code for the project has test drugs that adopt the protocols we filled out earlier, but doesn't have implementations of those methods yet. In the ChuckNorrisJokesTestServices, MockJokesService.swift file, implement the publisher function, but here just use a CurrentValueSubject. If the MockService is initialized with an Error, send the completion with a failure and otherwise just call erase AnyPublisher on the CurrentValueSubject publisher and return it. In ChuckNorrisJokeTestServices, MockTranslationService.swift do the same thing, but in the Error case, use dispatchqueue.global.asyncafter to wait a 10th of a second before sending the failing completion event. The delay here will be used when performing unit tests later on. Okay our publishers are in place. So let's use them first in the View Model, which we'll do in the next episode.