Learn how the timeout operators can be used to cause a publisher to time out and complete, and measureInterval to measure the interval between emissions.
This content was released on Feb 4 2021. The official support period is 6-months
from this date.
Learn how the timeout operators can be used to cause a publisher to time out and complete, and measureInterval to measure the interval between emissions.
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: 24. Debounce and Throttle
Next episode: 26. Challenge: Collect Values by Time
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.
The timeout operator is a special timing operator whose purpose is to semantically distinguish an actual timer from a timeout condition. Therefore, when a timeout operator fires, it either completes the publisher or emits an error you specify. In both cases the publisher terminates. Let's look at timeout in a demo. Switch to the timeout page in the playground. Add a pass through subject that sends values of type void which is actually a legitimate type. Just sending a notification that something happened. Apply a timeout operator on the subject that times out after five seconds and runs on the main queue. Make a new timeline view with the title button taps. (keyboard clicking) Make a V stack that has a button inside it. Make the button called the subjects send method when pressed and set the text of the button to press me within five seconds. Below the button place the timeline. Set the view to the playground's live view Finally, instruct the timed out subject to display events on the timeline. Run the playground. If you don't do anything within a five second interval, the publisher will timeout and complete. Run the playground again but this time tab the button at less than five second intervals. This time the publisher never times out, so it never completes. (mouse clicking) One last thing you can do here is have the publisher emit an error when the timeout happens. Make a new timeout error enum that extends error and give it a timed out case. Change the subject air type from never to time out air. Finally in the timeout operator call, add the custom error argument and pass in the .timed out case. Run the playground again and let the publisher timeout. You'll see that the timed out subject emits a failure. The last timing operator is Measure Interval. This operator doesn't manipulate time but just measures it. The measure interval using operator is used to find out the time that elapsed between two consecutive values emitted by a publisher. Let's look at it in a demo. Go to the measure interval page in the playground. Make a pass through subject that has a string input type and a measure interval operator on that subject. Run it on the main queue. Make two timeline views, one for emitted values and another for measured values. Add those to a V stack and set the playgrounds live view to a UI hosting controller that uses the V stack as the root. Instruct the publishers to display events and their respective timelines. (keyboard clicking) Make two sync subscriptions one for each publisher. Print the Delta time and emitted values to the console. Use the typing Hello World array from the playground supplemental source to feed the pass through subject. (mouse clicking) Run the playground. In the console you'll see the messages from each sink, showing the Delta time and emitted string and interval. The interval values look odd though. That's because they are in the form of a time interval and nanoseconds. You can fix that by updating the print statement. We run the playground and you'll see the values are now in seconds. You can also run on a different queue. Make a new measure interval subject to run on the main run loop. There's no need to make an additional timeline view. Make a new subscription to print the times from measure subject two. Run the playground. You'll see that the values are displayed directly in seconds but different from the intervals printed when running on the main queue. It's probably best to stick with dispatch queue for the scheduler, but in the end, it's up to you. You've made it through all the timing operators for this course. Let's recap. Delay, delays the values emitted by publishers so that you can see them later on 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. Debounce allows you to wait for values to accumulate before passing them downstream to a consumer. Throttle is like debounce but lets you run on other schedulers and emit either the first or last value, emitted from the publisher. Timeout sets up a timeout for the publisher if a value isn't sent within the specified time interval, the publisher times out and completes. And measure interval, measures the interval between emissions from the publisher. With all of those timing operators under your belt, it looks like a challenge is up ahead. Go take a quick break and when you're ready, come back to put your skills to the test.
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.