NEW: Got a question? Get help on our new forums! >>

How to cancel a delayed perform() call

Swift version: 5.1

Paul Hudson    @twostraws   

You can make a method call run after a number of seconds have elapsed using perform(_:withObject:afterDelay:), like this:

perform(#selector(yourMethodHere), with: nil, afterDelay: 1)

However, what if you change your mind, and decide you don't want yourMethodHere() to be called? As long as you act before that timer expires, you have two options: cancel that specific delayed call, or cancel all delayed calls.

To cancel that specific method call, you need to use the method cancelPreviousPerformRequests(withTarget:) on NSObject. Provide it with a target (where the method was going to be called), as well as the same selector and object you used when calling perform(), and it will cancel that delayed call.

For example:

// set up a delayed call…
perform(#selector(yourMethodHere), with: nil, afterDelay: 1)

// …then immediately cancel it
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(yourMethodHere), object: nil)

Being able to filter the cancellation by both selector and object means you can be very specific: "cancel the printing call for this filename."

If you've made a number of delayed calls and want to cancel them all – very helpful if you're about to leave a view controller, for example, and want to abandon any queued work – you can use this method call instead:

NSObject.cancelPreviousPerformRequests(withTarget: self)

That will cancel every call that was queued up on self, regardless of which selectors and objects were used.

If you're making delayed calls on a specific object, just use that object in place of self. For example:

myObj.perform(#selector(yourMethodHere), with: nil, afterDelay: 1)
NSObject.cancelPreviousPerformRequests(withTarget: myObj, selector: #selector(yourMethodHere), object: nil)
Hacking with Swift is sponsored by Instabug

SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred by integrating Instabug's SDK in one minute. You will automatically receive device data, network logs, and reproduction steps with every bug and crash report.

Learn more and get started for free

Sponsor Hacking with Swift and reach the world's largest Swift community!

Snapthread is a casual video editor and slideshow maker that makes discovering, compiling and sharing your favorite memories effortless.

Available from iOS 4.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5