← Back to list

Myths and Facts about NSNotification posting and receiving

Case 1 :-

Hadhi Abdul Kareem · 2017-09-13 05:56 · 12 claps · 1.9 min read
#ios #ios-development #notifications #nsnotification
Open on Medium ↗
Wiki topics: 📱 · Mobile Development

Myths and Facts about NSNotification posting and receiving

Case 1 :-

Myth Posting NSNotification is asynchronous. That is, if there are multiple recievers for the same notification, notification delivery happens asynchronously

Fact No.. Its not. All those happens synchronously. That is, the posting will not completed untill all the notification receivers returns. Yes, its kind of blocking.. So if one of the notification receiver executes a time taking task, it blocks other receivers and the notification posted thread

Case 2 :-

Myth Its OK to post the NSNotification from main thread -OR- we should always post the NSNotification from main thread

Fact Theoretically its fine but practically its not. Note that, the notification delivers on the same thread, which posted the notification. That is, notification receiver code executes on the same thread that posted the notification. For example, if the notification posted on main thread, the receiver handling function will run on the main thread even though the receiver object (which has the function) is created in another thread.

In that case, we should not make any assumptions that which thread is going to handle this notification. It can be any thread. So its better to post the notification from other threads than the main thread which will not block the main thread. The decision of selecting the appropriate thread should be handled by the receiver end.

An example at the receiver end:-

Case 3 :-

Myth Its absolutely fine to run the receiver method synchronously

Fact No, its not. As I mentioned already, posting the notification is a blocking one. So its better to asynchrnously handle the receiver ends so that, the caller(the one who posted the notification) will not get blocked. Another advantage is, while starting an asynchronous block, we can switch to appropriate thread/queue.

Conclusion

  • Its better to post the notification from other threads than main thread, even though you know that the receiver is going to handle it in main thread
  • At receiver end, you should handle the things in asynchronous manner and should return immediately
  • At receiver end, you can decide on which thread you should handle the things

Note that, this is not the hard coded rules but a standard way of doing it. Sticking to such things will always avoids future problems and make the app really extendible as the foundation code got some rules.


메타데이터
post_id
df7f5729b19f
slug
myths-and-facts-about-nsnotifcation-posting-and-receiving-df7f5729b19f
url
https://medium.com/@hadhi631/myths-and-facts-about-nsnotifcation-posting-and-receiving-df7f5729b19f
canonical_url
https://medium.com/@hadhi631/myths-and-facts-about-nsnotifcation-posting-and-receiving-df7f5729b19f
author_url
https://medium.com/@hadhi631
status
ok
fetched_at
2026-08-18 04:52:05