开发者

NSNotificationCenter Add?

开发者 https://www.devze.com 2023-02-27 03:51 出处:网络
I ha开发者_运维百科ve two view that when you switch from one to another, they call a notification to the view that\'s about to get loaded to refresh the content. The weird thing is that the first time

I ha开发者_运维百科ve two view that when you switch from one to another, they call a notification to the view that's about to get loaded to refresh the content. The weird thing is that the first time the view loads, it will call it once, the next time twice, and so on. I concluded that it's because they keep getting added every time the view loads. Since the dealloc never get's called it's still there and it will keep adding now.

So is there a way to check if the notification exists before getting added to fix this issue?

Here's what I have in my viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReloadGridNotification:) name:@"ReloadOHGridView" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReloadBadgeNotification:) name:@"reloadBadge" object:nil];

And my dealloc:

[[NSNotificationCenter defaultCenter] removeObserver:self];

Thanks!

Coulton

EDIT 1:

I show my views in a UINavigationController and switch between them. Here's my code to refresh the different view:

- (void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadBadge" object:self];
}


Every time you call addObserver, the notification center will add a entry to its internal structures. This means the notification center will call your observer once more every time you call viewDidLoad.

If your view is unloaded for any reason and then reloaded, then viewDidLoad will get called again. Your removeobserver will not get called until the object is destroyed, which may explain why your removeobserver did not work.

You should either check whether you have already called addObserver with a flag, or manually remove the observer with removeObserver when you unload your view in the viewDidUnload method.

Edit1: Alternatively, can you add the observers somewhere else, like in the App Delegate?


When memory gets tight the OS will dump your view, but will first call viewDidUnload; when it has to reload them it calls viewDidLoad. dealloc, however, is only called when all references to your view have been released, which likely doesn't happen until your app quits.

As a result, as @futureelite7 noted, you're adding a new observer every time your view is reloaded but, effectively, are never removing it.

All you need to do is ensure that the observer is added in viewDidLoad and removed in viewDidUnload you won't have the multiple notification problem. No need for a flag or for putting the observer anywhere else.

From your comments it sounds like you might have tried it, but I suggest going through your code and making absolutely certain you're only adding them in DidLoad and are always removing them in DidUnload. It works like a charm in app after app.


Edited to Add

Because your view keeps getting unloaded, which only happens if you do it manually, if all references to it are lost, or if memory gets tight, I suggest looking at all three to help ensure you're doing what you can to keep your view around.

0

精彩评论

暂无评论...
验证码 换一张
取 消