Each publisher gets the ability to retry sending their data if errors are encountered - something commonly seen when
working with getting data from the network. The retry() operator can be used to retry the publisher a certain number of times
before failing, and the catch() operator can be used to fall back to a default set of data if all else fails.
This content was released on Apr 13 2021. The official support period is 6-months
from this date.
Each publisher gets the ability to retry sending their data if errors are encountered - something commonly seen when
working with getting data from the network. The retry() operator can be used to retry the publisher a certain number of times
before failing, and the catch() operator can be used to fall back to a default set of data if all else fails.
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!
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.
There will be times in processing data where you'll need to retry if a failure is encountered. Networking is a great example here. If a download only partially completes you can use Combine's retry operator to retry the download. If other errors are encountered, including while we're trying, you could try catching those errors to keep your app running smoothly. Let's dive right into an example using retry and catch. Included in this playground sources folder is a photoservice.swift file that contains a mechanism to fetch a photo in either low or high quality using a custom publisher. However, when asking for a high quality image, it always fails. So you can experiment with techniques for handling errors. Making instance of the photo service. Make an example block that calls fetch photo on the photo servers and subscribe to the pipeline with a sink. In the receive value block, list the image and print a message to console. Run this in the playground and the console shows you the result of the fetch. You can even use the playground to look at the image. Change the quality from .low to .high and run the playground again. This time the console reports an error. Maybe we just need to try harder. Add a call to retry after the fetch photo call. You get a free retry mechanism for every piece of work in a publisher thanks to the retry operator. Before running this add a handle events call between the fetch photo and retry calls. This will help you see when the retries occur. Run the playground and you'll see the initial try and three retries. Update the fetch photo call to have an extra argument. Failing times. This will cause it to fail twice and then succeed on the third try. Run the playground again and this time the high quality version of the image is received. What if you want to fall back to a default image if the fetch fails? Remove the failing times argument from fetch photo and add a call to replace error after the call to retry. Run this in the playground and look at the result. Instead of showing an error, it displays the default image. What if you wanted to try to fall back to the low quality image if the fetch failed? Add a call to catch after the retry, but before replace error with. Run this in the playground. As before, an initial attempt is followed by three failures but then the catch operator comes in and requests a low quality version of the image.
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.