DispatchQueue.main.asyncAfter(deadline: .now() + 1) to schedule a method call one second later without blocking the main thread. In Objective-C, use performSelector:withObject:afterDelay: or schedule a block with dispatch_after.Why this happens
A touch handler runs immediately when UIKit delivers the event. If you call a second method directly from that handler, it runs immediately too; starting an animation does not automatically delay the following line of code.
Schedule the second call instead of pausing the thread. For a method that updates a UIImageView or other UIKit views, schedule it on the main queue. The delay is a minimum wait, not a guarantee that the method will run at precisely the one-second mark: the main queue must also be available to execute it.
How to fix it
1. Schedule the call on the main queue in Swift
Use DispatchQueue.main.asyncAfter when the delayed method updates the interface. Put the call inside the closure, and specify the delay in seconds with .now() + 1.
import UIKit
class ViewController: UIViewController {
func handleImageTouch() {
// Start the touch animation here.
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
self?.finishTouchResponse()
}
}
private func finishTouchResponse() {
// Update the interface here.
}
}
Call handleImageTouch() from your existing touch or gesture handler. The handler returns without waiting, so the main thread can continue processing events and drawing the interface. The closure becomes eligible to run after one second.
The [weak self] capture prevents the pending closure from keeping the view controller alive solely until the delay expires. If the controller has been released by then, self is nil and the method is not called. Remove the weak capture only if retaining the controller until the call runs is the behavior you want.
This delay starts when you schedule the closure, not when an animation finishes. If the main queue is busy, the actual call may occur later than one second.
2. Use a delayed selector in Objective-C
For an Objective-C method with no arguments, performSelector:withObject:afterDelay: is a concise option. Schedule it from the main thread if the method changes UIKit views:
[self performSelector:@selector(finishTouchResponse)
withObject:nil
afterDelay:1.0];
Implement the method on the same object:
- (void)finishTouchResponse {
// Update the interface here.
}
The delay is in seconds. This API schedules the selector on the current thread’s run loop, so use it on the main thread for UI work. It is not a substitute for dispatching work to a background queue.
If the delayed call is no longer needed, cancel the matching pending request before it runs:
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(finishTouchResponse)
object:nil];
The target, selector, and object must match the scheduled request. Cancellation cannot undo a method that has already executed. A pending delayed selector retains its target, so do not rely on the target’s dealloc method as the only place to cancel it; cancel when the relevant operation or screen is no longer needed.
3. Use a dispatch block in Objective-C
A block is useful when the delayed work needs to use local values or when you prefer to keep the scheduling code and call together. This is the Objective-C counterpart to the Swift main-queue example:
#import <dispatch/dispatch.h>
__weak typeof(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC),
dispatch_get_main_queue(), ^{
[weakSelf finishTouchResponse];
});
As in the Swift example, the weak reference allows the target to be released before the delay expires. If that happens, sending the message to the resulting nil reference has no effect.
For work that does not touch UIKit, a global queue may be appropriate instead. If that work produces a result that must be displayed, dispatch the interface update back to the main queue. Choosing a background queue is not necessary for a small UI update, and it does not make UIKit safe to access off the main thread.
4. Use animation completion when timing depends on the animation
A fixed delay and an animation completion answer different questions. Use the delay when the requirement is to wait about one second after the touch. If the requirement is instead to run code when a UIView animation finishes, use its completion handler:
UIView.animate(withDuration: 1.0, animations: {
self.imageView.alpha = 0.5
}, completion: { [weak self] finished in
guard finished else { return }
self?.finishTouchResponse()
})
This example belongs inside a controller that has an imageView property and a finishTouchResponse() method. The finished value indicates whether the animation completed without interruption. If your animation does not provide a usable completion callback, keep the scheduled-delay approach and choose the delay independently.
Avoid sleeping the main thread to create the delay. Blocking it prevents UIKit from processing interface work while the thread sleeps, which can make the app appear frozen.
How to prevent it
- Schedule UIKit changes on the main queue or main thread.
- Use an animation completion handler when the next action depends on animation completion rather than elapsed time.
- Decide whether the delayed closure should retain its owner or skip the call if that owner is gone.
- Cancel pending delayed selectors when their work is no longer relevant.
- Treat a one-second delay as a minimum, not as a precise deadline.
FAQ
Does asyncAfter block the main thread for one second?
No. It schedules the closure and returns immediately. The closure itself runs on the main queue after the delay, so keep the work inside it appropriate for that queue.
Will the method run exactly one second later?
Not necessarily. It becomes eligible after the specified delay, but other work on the queue can postpone its execution. A busy main thread can also delay a selector scheduled on the main run loop.
Should I use a delay or an animation completion handler?
Use a delay when the action should occur after an elapsed interval. Use a completion handler when the action should follow the end of an animation and that animation provides a suitable completion callback.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.