Posting NSNotification on the main thread
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
NSNotificationCenter does not automatically move notifications onto the main thread. A notification is delivered on the same thread where it is posted, so if observers update UI or other main-thread-only state, you either need to post from the main thread or dispatch the observer's work back there.
The Important Rule
Notification delivery is synchronous by default for in-process observers. That means this code posts on whatever thread runs it:
If that line runs on a background queue, the observers are invoked on that background queue too.
That is not inherently wrong. It becomes wrong only when an observer assumes it is safe to touch UIKit or AppKit state from there.
Posting on the Main Thread Explicitly
If the notification is conceptually a UI event and observers are expected to update interface state, post it on the main queue.
The same idea in Swift looks like this:
This guarantees the observers receive the notification on the main thread.
When Posting on the Main Thread Is Necessary
Post on the main thread when observers are expected to:
- update UI elements
- trigger view controller logic that assumes main-thread execution
- interact with thread-affine frameworks
If the notification is purely about background data processing and observers are written to be thread-safe, posting on the main thread is not required.
That distinction matters. Not every notification needs to be forced onto the main queue.
Another Valid Pattern: Post Anywhere, Redispatch in the Observer
Sometimes the poster should stay background-only and the observer should decide whether main-thread handling is needed.
This pattern keeps the posting side simple and pushes the thread decision to the consumer.
If you use the block-based observer API, you can also register with OperationQueue.main so the observer block itself is scheduled on the main queue. That can be cleaner when the observer is always UI-bound.
Avoid Confusing Notification Delivery with Operation Queues
When you use selector-based observers or default block observers, the notification center does not magically create a main-thread handoff for you. If you need a guaranteed delivery context, you must manage it explicitly.
That is why many teams treat notifications as thread-neutral messages and document whether observers should expect background or main-thread delivery.
Common Pitfalls
The biggest pitfall is assuming that because notifications often lead to UI updates, the notification center must already be dispatching on the main thread. It does not.
Another issue is overusing main-thread posting for notifications that are purely computational. That can create unnecessary UI-thread pressure.
Developers also sometimes fix the poster but forget other code paths post the same notification from background work. If thread expectations matter, make the convention explicit across the codebase.
Finally, remember that synchronous notification delivery means observers run immediately during post. If an observer is slow, the posting thread is blocked until it returns.
Summary
- Notifications are delivered on the thread that posts them.
- Post on the main thread when observers are expected to perform UI work.
- Otherwise, observers can redispatch to the main queue only when needed.
- '
NSNotificationCenteris thread-aware but not thread-relocating.' - Be explicit about threading expectations so notifications do not become a hidden source of UI-thread bugs.
Related reading
- #pragma mark in Swift?
- pragma mark in Swift?
- Precision string format specifier in Swift
- PreferenceManager getDefaultSharedPreferences deprecated in Android Q
- preferredStatusBarStyle isn't called
- Present and dismiss modal view controller
- Presenting a UIAlertController properly on an iPad using iOS 8
- Presenting modal in iOS 13 fullscreen
.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.