Posting nsnotification but observer is not hearing it - ios

I am using NSNotifiationCenter, like a 1000 other times, and post a notification, but my observers are not hearing it? What gives?
=== THIS IS IN MY TABLEVIEW ===
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleSuccessSocket)
name:kNotificationServiceStartSuccessful
object:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleFailedSocketConnection)
name:kNotificationSocketFailedToConnect
object:self];
}
=== THIS IS IN MY SOCKETMANAGER (socket manager is a singleton if that matters) ===
-(void)willStartService {
....
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationServiceStartSuccessful object:nil];
....
}
I have debugged and viewDidLoad is being called in my view. Yes, my willStartService is being called.

You are registering the TableView only for notifications sent by itself. It should be..
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleSuccessSocket)
name:kNotificationServiceStartSuccessful
object:nil];

Try setting object:nil in your -addObserver:selector:name:object method calls. What you're doing is telling Notification Center to ONLY listen for notifications that come from the table view instance.
If you don't want to pass nil you'll have to pass an instance of your socket manager.
Check the Docs:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSNotificationCenter/addObserver:selector:name:object:

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.

viewDidUnload alternative iOS

Which method can I use to catch the event when I move to another view?
What I have tried so far and didn't work: viewWillDisappear:, willMoveToParentViewController:, dealloc:
I am trying to unregister an observer for notifications.
The viewDidUnload method is now Deprecated.
If you want unregister an observer for notification, try in dealloc method:
E.g.
(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
//or
[[NSNotificationCenter defaultCenter] removeObserver:self
name:#"notifiactionName" 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];
}

Observer never gets removed from NSNotificationCenter

I'm adding a view controller as an observer for UIKeyboardWillShowNotification notification.
I have this code in my viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
And in my dealloc:
[[NSNotificationCenter defaultCenter] removeObserver:self];
The observer is not being removed even though dealloc is called when the view controller is closed. So when I open it for the second time, NSNotificationCenter will attempt to notify the old object, which is released, and the app crashes.
I have seen several questions here on StackOverflow about this particular problem, but non of the answers works for me.
I've tried removing the observer in viewWillDisappear and viewDidDisappear but the same problem happens.
I'm using ARC.
Have you tried this exact chunk of code in viewWillDisappear?
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
From your explanation I don't think the problem is with the removal of the observer.
Try to trigger the Observer from another viewcontroller. If it's not triggered you'll know the removal is successful and the problem occurs when you are adding the observer the second time.
Maybe try by specifying the parameter name that you have set before like this :
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
Looks like the observer is getting set multiple times. Is your controller inherited from a class which also registers for same notification? That could lead to controller instance getting registered as observer more than once. As a workaround try this, in your controller class where you add the observer,
// Remove as observer first
[[NSNotificationCenter defaultCenter] removeObserver:self];
name:UIKeyboardWillShowNotification
object:nil];
// Then add
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
This will ensure that the observer is getting added only once.
Hope that helps!
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"name" object:nil];
it works fine with me

Resources