How can I be notified when a dispatch_async task is complete?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
dispatch_async itself does not return a completion signal. It just submits work to a queue and returns immediately. If you want to know when that work is done, you usually add your own completion callback, use a DispatchGroup, or chain back to another queue when the background work finishes. The right choice depends on whether you are tracking one task or coordinating several.
Add an Explicit Completion Closure
For a single async operation, the simplest pattern is to wrap the work in a function that accepts a completion closure.
This is a common and clear pattern:
- run the expensive work on a background queue
- hop back to the main queue if needed
- call the completion closure
That is usually enough when there is one unit of work and one caller waiting for it.
Use DispatchGroup for Multiple Tasks
If you need to be notified after several async tasks finish, DispatchGroup is the standard tool.
notify runs once the group’s outstanding tasks have all called leave.
This is much cleaner than manually counting completions with shared state.
Use DispatchWorkItem for a Single Work Unit
Another option is DispatchWorkItem, which lets you package a unit of work and observe it more explicitly.
This is handy when you want the work itself to be a first-class object that can be submitted, canceled, or observed.
Avoid Blocking with wait
DispatchGroup also offers wait, but that blocks the current thread. If your goal is notification, prefer notify.
Blocking is usually the wrong answer in UI code because it can freeze the app and defeats the point of asynchronous work.
So:
- use
notifyfor asynchronous completion handling - use
waitonly when blocking is genuinely intended and safe
In practice, notify is almost always the better match for app code because it preserves the asynchronous structure instead of temporarily turning it back into synchronous waiting.
Update UI on the Main Queue
If the completion triggers UI updates, return to the main queue first.
This matters because UIKit work belongs on the main thread.
Common Pitfalls
The biggest mistake is expecting dispatch_async itself to provide a built-in callback. It does not. You create the completion path yourself.
Another issue is forgetting group.leave() in one branch of async work. That causes notify never to fire.
Some developers also use wait on the main thread, which can freeze the app.
Finally, if the completion touches UI, do not call it from a background queue unless the caller explicitly expects that behavior.
Summary
- '
dispatch_asyncdoes not provide automatic completion notification on its own.' - For one task, add an explicit completion closure.
- For several tasks, use
DispatchGroupandnotify. - '
DispatchWorkItem.notifyis another useful completion pattern for packaged work items.' - Return to the main queue before updating UI after background work finishes.
Related reading
- How can I call this async method in my Xamarin Forms when my app starts?
- How can I confirm if an async EF6 await db.SaveChangesAsync worked as expected?
- How can I convert this foreach code to Parallel.ForEach?
- How can I corral a method which may contain it's own async calls without having write access to the file?
- How can I build a URL with query parameters containing multiple values for the same key in Swift?
- How can I change a SwiftUI Color to UIColor?
- How can I Define an Async Action in c?
- How can I determine what core a Java thread is running on?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.