NSNotification
main thread
iOS development
Objective-C
programming tips

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.

Browse interview questions

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:

objective-c
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataReady"
                                                    object:self];

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.

objective-c
1dispatch_async(dispatch_get_main_queue(), ^{
2    [[NSNotificationCenter defaultCenter] postNotificationName:@"DataReady"
3                                                        object:self];
4});

The same idea in Swift looks like this:

swift
DispatchQueue.main.async {
    NotificationCenter.default.post(name: .dataReady, object: self)
}

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.

swift
1NotificationCenter.default.addObserver(
2    forName: .dataReady,
3    object: nil,
4    queue: nil
5) { notification in
6    DispatchQueue.main.async {
7        // Safe UI update here
8    }
9}

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.
  • 'NSNotificationCenter is 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.