NSNotificationCenter Thread breaks at second call even after it has been removed - ios

I am using NSNotificationCenter for two different ViewControllers. First time calling NSNotificationCenter works well and after that I remove the observer. But if I run it again, the thread breaks even after removing the observer.
in ViewController1:
[[NSNotificationCenter defaultCenter] postNotificationName:#"textUpdateNotification" object: nil ];
in ViewController2:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receiveNotification:) name:#"textUpdateNotification" object:nil];
-(void)receiveNotification:(NSNotification *)notificaton
{
....
....
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"textUpdateNotification" object:nil];
}
I tried with removing the observer in - (void)dealloc there also the same thing is happening.

Try removing the observer in viewDidDisappear
-(void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"textUpdateNotification" object:nil];
}

Sorry for my typo,
I am removing observer in the source ViewController where I am triggering the NSNotification, I should remove it from second ViewController where its being used.
in ViewController1:
[[NSNotificationCenter defaultCenter] postNotificationName:#"textUpdateNotification" object: nil ];
in ViewController2:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receiveNotification:) name:#"textUpdateNotification" object:nil];
-(void)receiveNotification:(NSNotification *)notificaton
{
....
....
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"textUpdateNotification" object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"textUpdateNotification" object:nil];
}
Thanks for every one

Related

Observer in UITableviewCell

In my custom UITableViewCell I added an observer in NSNotificationCenter:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(stopActivityIndicator) name:#"stopActivityIndicator" object:nil];
I post a notification in a UIViewController:
[[NSNotificationCenter defaultCenter] postNotificationName:#"stopActivityIndicator" object:nil];
This function "stopActivityIndicator" is not being called, any idea what causes this?
EDIT:
ExploreViewController.m
-(void)showCorrectBannerAfterPlusButtonClicked:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"stopActivityIndicator" object:nil];
}
ExploreViewController contains a UITableView with ExploreTableViewCells.
ExploreTableViewCell.m
- (IBAction)plusButtonClicked:(id)sender
{
self.plusButton.hidden = YES;
[self.plusButtonActivityIndicator startAnimating];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(stopActivityIndicator) name:#"stopActivityIndicator" object:nil];
}
-(void)stopActivityIndicator
{
_plusButton.hidden = NO;
[self.plusButtonActivityIndicator stopAnimating];
[[NSNotificationCenter defaultCenter]removeObserver:self name:#"stopActivityIndicator" object:nil];
}
Ok, question, when do you call "showCorrectBannerAfterPlusButtonClicked"?? As this is the method where you post the notification observed by the tableViewCell.
What I see is the notification observer adding and removal, but I don't see how the UIViewController knows when the cell's "plusButtonClicked" is called, so perhaps the method posting the notification is not being called.
Also, be careful with the cell reusage, have in mind if you should remove the observer at that point.

Receive NSNotification method not calling

I have use below for for register notification:-
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(AllSearch:) name:filter object:nil];
-(void)AllSearch:(NSNotification *)notification
{
NSLog(#"abc");
}
and i post notification with this code:-
[[NSNotificationCenter defaultCenter] postNotificationName:filter object:nil];
But "AllSearch" Method not calling.
plz check you code you have to addObserver first
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(AllSearch:) name:filter object:nil];
befor postNotification
[[NSNotificationCenter defaultCenter] postNotificationName:filter object:nil];

I cannot remove observers that I register

I am registering some observers in my application to show a controller when timeout occurs:
for(Ad* ad in ads){
if(ad.published){
[ad resetTimer];
[[NSNotificationCenter defaultCenter] removeObserver:ad name:#"TouchBegan" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:ad selector:#selector(resetTimer) name:#"TouchBegan" object:nil];
}
}
In Ad class, I try to remove observers in dealloc:
-(void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"TouchBegan" object:nil];
}
But it seems that there still exists an observer after dealloc.
Ads array is a property of a Shop class:
#property(nonatomic, strong) NSArray<Ad>* ads;
How I can completely remove observers that I register?
It is safe to remove self as observer for all names in dealloc [[NSNotificationCenter defaultCenter] removeObserver:self];
If the observer is added to a view controller, I strongly recommend adding it in viewWillAppear and removing it in viewWillDisappear.
[[NSNotificationCenter defaultCenter] removeObserver:someObserver]; will remove even the super class observers which is highly unrecommended (except in dealloc because the object is unloaded) but in viewWillDisappear you should remove the observers one by one by using [[NSNotificationCenter defaultCenter] removeObserver:self name:#"TouchBegan" object:nil];

NSNotificationCenter calling two times

Below is what I have.
MainViewController.m
- (IBAction)sideMenuAction:(id)sender {
NSLog(#"login==sideMenuAction");
[[NSNotificationCenter defaultCenter] postNotificationName:#"ShowMySideMenuNotification" object:self];
}
NotificationListener.m
-(void)viewDidLoad {
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"ShowMySideMenuNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(adjustShowMenu) name:#"ShowMySideMenuNotification" object:nil];
}
-(void) adjustShowMenu {
NSLog(#"notification adjustShowMenu=");
}
Now when I click side menu button in MainViewController, what I was expecting is call adjustShowMenu from NotificationListener once, however it is called twice.
Below is the NSLog for the same.
2015-01-20 12:27:30.798 abc[699:169314] login==sideMenuAction
2015-01-20 12:27:30.798 abc[699:169314] notification adjustShowMenu=
2015-01-20 12:27:30.799 abc[699:169314] notification adjustShowMenu=
What I was expecting is
2015-01-20 12:27:30.798 abc[699:169314] login==sideMenuAction
2015-01-20 12:27:30.798 abc[699:169314] notification adjustShowMenu=
Any idea what is going wrong?
Note: I also tried in viewDidAppear instead of viewDidLoad, but its giving same result.
When I searched online, many answers asked to removeObserver. I did same, but still twice notification is getting called.
As per answer here, I make changes as below and its working fine now.
-(void) viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(adjustShowMenu) name:#"ShowMySideMenuNotification" object:nil];
}
-(void) viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"ShowMySideMenuNotification" object:nil];
}

Using instance methods in the app delegate

I wanted to run a instance method from my BPGameController class called "pauseGame" in my app delegate when my application enters background mode and when it resigns, and a instance method called "resumeGame" when the application becomes active again, I've tried a few different things but none have worked for me so far
In BPGameController's viewDidLoad method sign up for the notifications as follows:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(pauseGame)
name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(resumeGame)
name:UIApplicationDidBecomeActiveNotification object:nil];
When you are done remove yourself from observing.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];

Resources