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

24. Debounce and Throttle

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: 23. Delay and Collect Next episode: 25. Timeout and measureInterval

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.

When designing user interfaces, you want to update some views when a user enters data in a text field. However you don't want to respond to every single keystroke the user makes, you want some mechanism to wait until the user stops typing. This is where debounce comes into play. Let's look at debounce in a demo. Starting in the debounce page make a pass through subject, used the balance on the subject to wait for one second on emissions from subject, then it will send the last value sent in that one second interval, if any. This has the effect of allowing a max of one value per second to be sent. Since you're going to subscribe multiple times to debounce and you want it to be consistent, you share to create a single subscription point to debounce that will show the same results at the same time to all subscribers. And the built-in source code in the playground there is an array that will help simulate a user typing. Make timeline views to help visualize the sequence of values admit it from the publishers, place them in a V stack and set the line view you for the playground. Send events to the timeline views from the publishers. For this demo you'll add one more thing to the playground add two subscriptions that will print out the values to the console. Finally feed the array of simulated typing to the subject to start the process. Run the playground, you see the visual displays in the playground live view as well as the values in the console, both the debounced timeline view and the console showed that the values got debounced after the user types hello and world. Throttle is similar to debounce functionally but with some key differences, you can delay for a certain amount of time as you did with debounce, you can also emit values to a particular scheduler, and you can specify whether to emit the latest value received from the subject. Let's look at throttle in a demo, define a throttle delay constant that you'll use later then make a pass through subject which emits strings. Apply the throttle operation to the subject. With these settings on the throttle code, the throttled subject will now only met the first value received from the subject during each one second interval, because you set latest to false. Remember that adding the share operator here guarantees that all subscribers see the same output at the same time from the throttled subject. Make timeline views to visualize the original and throttled publishers, set the live view of the playground and tell each publisher to display events in the respective timeline views. Try to understand what's going on make another subscription to print out the values admitted by each publisher. Feed the subject with the typing hello world array this simulates a user typing. Run the playground. It doesn't look too much different than debounce the console however shows a different picture. You see an extra throttle emission around 2.2 seconds showing hello W which was the first value admitted by the subject since the last throttle. You also see a similar emission at 1.1 seconds showing H E finally, change the argument of latest to true. Run the playground. The upper looks familiar doesn't it? Values are admitted at 1.1 and 3.2 seconds hello and hello world. This is like the debounce but the debounce is delayed from the pause. Also take note that the value emitted by the throttle at approximately 2.2 seconds is now hello wor, including the latest value emitted from the subject publisher since the last time a throttle was admitted. We looked at two similar timing operators in this episode. Debounce allows you to wait for values to accumulate before passing them downstream to a consumer. And throttle is like debounce but lets you run on other schedulers and admit either the first or last value emitted from the publisher. You've got two more timing operators to learn about in this next episode, time-out and measureinterval. See you then.