Reactive Programming in iOS with Combine

Feb 4 2021 · Swift 5.3, macOS 11.0, Xcode 12.2

Part 4: Timing, Scheduling and Sequencing Operators

23. Delay and Collect

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: 22. Introduction Next episode: 24. Debounce and Throttle

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.

Have you ever heard the saying timing is everything? This is especially true in combine, which models asynchronous event flows over time. It's no surprise then that combined has a set of operators that deal with time, in particular, how sequences react to and transform values over time. In this episode, you'll learn about two timing operators, delay and collect. The most basic time manipulation operator is delay. This operator, delays the values emitted by a publisher so that you can see them later than they actually occur. The delay for tolerance schedule or option operator time shifts a whole sequence of values. Each time the publisher emits a value, delay keeps a hold of it for the specified duration, then emits it downstream. This happens on the scheduler you specify. Let's take a look at this in a demo. To better visualize what is going on with timing operators, you'll use an animated X code playground, that visualizes how data flows over time. In the starter playground for this project you'll find that it is divided into several pages. Each page will exercise one or more operators in this episode. The playground also includes some ready-made classes, functions and sample data that will come in handy. Enable show rendered markup in the playground. If you aren't already. And you'll see a next link that will take you to the next page. To get the most benefit from this playground, make sure the live view is enabled by using the buttons in the upper right-hand corner of the editor. Also remember that playgrounds can be run automatically or manually. If you choose manually run here, you can use the play button to run the playground. If by chance the playground doesn't run properly you can go to preferences, locations and click the arrow next to the drive data location. This will show the drive data folder and finder, quit X code, move the drive data folder to the trash and then restart X code. Your playground should now work properly. Now with that delay, let's talk about delay. Define a few constants to use. Create a publisher that emits a value every second and delays it by 1.5 seconds. Display both timelines simultaneously to compare them. To accomplish this, add a pass through subject which will take in dates, emitted by a timer and call it source publisher. Next make the delayed publisher which delays values emitted by the source publisher and emits them on the main scheduler. Create a timer that delivers one value per second on the main thread. Started immediately with a call to auto connect and feed the values to the source publisher. Now create the two views you will display in the playground, using the provided timeline view. Finally create a V stack to hold both timelines and set up the live view for this playground. Run the playground. The timelines are empty, feed them values and minimize each publisher. Add this code to the playground. Run the playground again. You can clearly see the shift in the delayed values relative to the original emission times. There may be cases where you want to collect a series of values over a period of time before performing an operation. This is where the collect by time operator comes into play. Go to the collect page of the playground and let's look at that operator in a demo. Start by defining some constants for the number of values per second, and the collect time. Make two publishers. The first as a pass through subject that will emit values published by a timer. The second publisher collects values from the source publisher and amidst those values on the main queue scheduler collecting values over a stride, defined by collect time stride. Use a timer to emit values at regular intervals on the main scheduler, using auto connect to start it right away and send the events to the source publisher. Make the timeline views like the delay examples, so you can see the meta values and the collected values side by side. Place the views and a V stack to display them on screen and set the live view for the playground. Finally, feed the timeline view events from the publishers. Run the playground. The source publisher emits every second, while the collected value shows an emission every four seconds. But what exactly is admitted? Go back and update the collective publisher to include a flat map operation. Every time collect emits a group of values, a collected flat, mat breaks it down again to individual values but admit it all at the same time. To this end, it uses the publisher extension of collection that turns a sequence of values and to a publisher emitting immediately all values in the sequence at individual values. Rerun the playground, and now you can see all the collected values. In this episode, you learned about two timing operators; delay delays the values admitted by a publisher so that you can see them later than they actually occur, and collect collects a series of values over a period of time before performing an operation. Which is useful when performing operations like averages. In the next episode, you'll learn about two other timing operators, the bounce and throttle.