Your app has to get data from a source in order to use it. The Publisher-Subscriber paradigm seen in Combine
seems like a natural fit here, so in this episode, learn how to create custom publishers to help deliver jokes
and translations to your model.
This content was released on Aug 5 2021. The official support period is 6-months
from this date.
Your app has to get data from a source in order to use it. The Publisher-Subscriber paradigm seen in Combine
seems like a natural fit here, so in this episode, learn how to create custom publishers to help deliver jokes
and translations to your model.
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!
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.
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.
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.