Will NSNotificationCreation impact on application performance? - ios

In my code i have a lot of NSNotification around 200+ .
[[NSNotificationCenter defaultCenter] addObserver....];
[[NSNotificationCenter defaultCenter] postNotificationName...];
Will this impact on my application performance if I register and fire notification a lot?

This is depend on how to handle NSNotificationCenter in your code.if you handle your NSNotification purely then its effect bcoz its called twice or more times..
best approach for NSNotification is you add in your viewWillAppear and remove it on viewWillDisappear.

Instead of NSNotificationCenter you should have a look at tolo
The biggest advantage is that you don't have to care about adding/removing observers - tolo does this for you automagically.
ps. failing to remove any observer might lead to retain cycles and memory leaks

Related

iOS UIApplicationWillEnterForegroundNotification Without Memory Leak?

I currently make use of this method in my objective-C app:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleEnteredForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
If I don't remove it with
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
am I at risk of a memory leak? Or is there a good way to utilize this notification without the risk of a memory leak?
There is no danger of a memory leak; the notification center's reference to your observer self is weak.
But there is a danger — namely, that self will go out of existence and that the notification center will later attempt to send it a notification. This will cause a horrific crash, one that is very difficult to track down (dangling pointer).
That is why you must be certain to unregister your observer in iOS 8 and before.
Starting in iOS 9, however, this ceases to be a problem, because the notification center's reference to your observer is not simply weak but is ARC-weak. This means that the reference to a released observer becomes nil. The notification center detects this and stops sending notifications to it, safely.

NSNotificationCenter - Observer selectors not being called

I've gone from having all my observers selectors of multiple NSNotifications being called to none of them working. Is there an XCode/Application setting that I may have disabled that could cause this behaviour?
This issue affects just about every class that I have registered as an observer. Everything was working fine a few days ago. I'm setting the observers mainly in viewDidLoad and removing them in dealloc.
Just to add: All notifications are being posted as I have already tested for this.
I ended up figuring it out. I can't believe I missed this.
The common super class for all observers was removing itself as an observer for all notifications in viewDidDisappear.
I have now changed this to only remove itself as an observer for a specific NSNotification using [[NSNotificationCenter defaultCenter] removeObserver:name:object:];

Is there a way to check if observer listens to some NSNotification?

I want to check if my view is listening for UIApplicationWillResignActiveNotification or not. If it is listening then I want to remove it during dealloc. Now I was wondering if there is way to do this using objective c ?
I am not trying avoid multiple additions for notifications. Here is bit more explanation of what I am trying to do.
I have custom gridView. I can initialize it with either scaling enabled or scaling disabled. If init with scaling enabled I add itself as observer of UIApplicationWillResignActiveNotification but if its init with scaling disabled then it does not add itself as an observer for that notification. Now, in dealloc I want to remove that gridView as an observer of that notification. So I was wondering if there is way to find out if gridView is listening to that notification or not.
I don't know of any way to check what notifications your observer is listening for, but regardless of whether it's listening for UIApplicationWillResignActiveNotification or not, calling:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification];
will cause self to stop listening for that notification, or do nothing if self is not listening for it.
Specifying the name of the notification you want to stop listening for is the best practice, but since you said you're putting this in dealloc, it would also be safe to just do this:
[[NSNotificationCenter defaultCenter] removeObserver:someObserver];
If you want to check in dealloc method, if your view is registered as observer to correctly remove it - you should not. All you need to do is:
[[NSNotificationCenter defaultCenter] removeObserver:myView]
and it will remove observers for all notifications you subscribed
NSNotificationCenter does not support this out-of-the-box. You have the same problem with KVO.
Generally one just keeps track of whether or not an object has been registered using a boolean property and unregisters only if this boolean has been set.

Recursive Notifications

My application is IOS application.
I have a scenario typical to the calendar iPad application, when the user goto to a date from a view, each views update itself and notify others. The problem is that notifications between views never ends.
I am using the NSNotificationCenter
[[NSNotificationCenter defaultCenter] postNotificationName:SelectedDateChangedNotification object:self userInfo:nil];
I would like to explore some ideas of how effectively implement this scenario.
Thanks
You need to have two ways of updating each of your views. The first way is when the user updates it and the second way when it gets updated by a different view. The first way sends notifications to the other views and the second way does not. From an implementation standpoint, the first way would just be a wrapper around the second way which first notifies the other views.

If add an observer for a notification in the AppDelegate, do I need to bother removing it?

In the AppDelegate's didFinishLaunchingWithOptions:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(contextChanged:)
name:NSManagedObjectContextDidSaveNotification
object:nil];
This is so I can merge changes to the data from other threads.
Question: Do I need to bother removing this listener in applicationWillResignActive or applicationWillTerminate? It doesn't seem like there's a point. I guess I'm asking if it's normal to have listeners like this in the main loop that never get removed.
You can never remove it, but if your app receive a notification (it won't happen in this case) while it is in background the notification will be queued and delivered to the application when it comes up again (if the app isn't killed ofc).
If don't want notifications that happen when your app is in background to be delivered once it comes up you can remove the listener in the methods you pointed out.
In this case, actually, it doesn't matter.

Resources