iphone - How to stop the Observer in NSNotification to called twice? -
i have observer of nsnotification
called twice. not know it.
i googled no solution found.
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(connectedtoserver:) name:@"connectedtoserver" object:nil]; - (void)connectedtoserver:(nsnotification*)notification { [[nsnotificationcenter defaultcenter] postnotificationname:@"sendmessagetoserver" object:message]; }
solution 1: first thing check if notification posted twice.
solution 2: if notification posted once, action called many times you've added observer notification (no matter notification same or not). example, following 2 lines register observer(self
) same notification(aselector
) twice.
[[nsnotificationcenter defaultcenter] addobserver:self selector:aselector name:aname object:nil]; [[nsnotificationcenter defaultcenter] addobserver:self selector:aselector name:aname object:nil];
you have find adding observer second time, , remove it. , make sure code add observer not called twice.
solution 3: if not sure whether have added observer or not, can following. make sure observer added once.
[[nsnotificationcenter defaultcenter] removeobserver:self name:aname object:nil]; [[nsnotificationcenter defaultcenter] addobserver:self selector:aselector name:aname object:nil];
Comments
Post a Comment