Modern Concurrency: Getting Started

Oct 18 2022 · Swift 5.5, iOS 15, Xcode 13.4

Part 2: Asynchronous Sequences

15. Using Combine

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: 14. Canceling Tasks Next episode: 16. Concurrent Downloads

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.

Make sure the core server is running and continue with your project from the previous episode or open the starter project for this episode. In this episode, you'll add a timer to show how long a download is taking. You'll use Apple's timer class, which is much easier to use now that it has a combined publisher. Combine is Apple's reactive programming framework. It's a huge topic with it's own book and video courses. You'll find links to these below this video window. The basic combined concepts are publishers and subscribers. Combine publisher asynchronously emits values over time and can complete with success or failure, just like an A sync sequence. And, it's easy to use, combined with swift concurrency. In "download view", import, combine. Add a state property to store the timer task so you can cancel it. A timer task should start when a download starts. A good place to detect this is when you store a task in download task. Add a "did set accessor" to download task. Add some housekeeping code in this did set closure. You cancel any currently running timer task. Then, if there's a download happening, store the start time so you can calculate the duration. Now for your combined timer. You'll use the timer Static Method Publish to get a timer publisher object, which emits the current date on the interval you specify. Create a timer sequence. You create a timer publisher that emits the current date every second on the main run loop in any of the usual modes: default, modal, and event tracking. Add another modifier. Instead of manually connecting to this publisher, you tell it to start ticking automatically whenever someone subscribes to it. Next, convert the publisher's value into a duration value. MAP calculates the elapse time and seconds and returns to the duration as a strength. Finally, and most importantly, you need an A sync sequence of duration values. Values returns an asynchronous sequence of the publisher's events, which you can loop over as usual. Time to iterate. Still in the did set accessor, create a new asynchronous task stored in "Timer Task" and loop over the sequence. In fact, you can use 4-0-8 with any combined publisher by accessing its values property, which automatically wraps the publisher in an A sync sequence. You can test this now, but first take of stopping the timer when it shouldn't be running. First, make the timer stop when the download ends. In "Download with updates" action, add this line after you set "is download active defaults". Also, cancel the timer task when the user taps "Cancel now". In the toolbar, add the same line to the button "Action". Build and run. Select the file, tap "gold". Let the file download completely. The timer stops when the image appears. Go back and start another "Go" download. Use a TIF file this time. Tap "cancel now" before it completes and the timer also stops. Congratulations, you've used a combined publisher to add a download timer to your app. In the next episode, you'll implement the premium download plan, Cloud Nine, using concurrent partial downloads.