开发者

NSNotificationCenter receive event method is executing multiple times

I am basically a Java developer and recently assigned a task on objective C (iPhone).

I have started coding in the language. but while implementing the NSNotificationCenter I am having very strange problem.

It is very difficult to explain my problem.

My class A is having one global variable named array of type NSMutableArray Pointer. class's init method looks something like

- (id) init  
{  
  if(self = [init])  
  {  
    [NSNotificationCenter defaultCenter] addObserver:self @selector(successLogin) name:"successLogin" object:nil];  
    [NSNotificationCenter defaultCenter] addObserver:self @selector(failureLogin) name:"failureLogin" object:nil];  
   ... <some code>开发者_如何学JAVA;  

}

recieve event method looks like

- (void) successLogin: (NSNotification * ) notification
{  
   ... <some code of writing data to db using **array**>  
   [self showSuccessAlert]; // it is showing UIAlert
}

The sendEvent method (of other class B) is having code something like

[[NSNotificationCenter defaultCenter] postNotificationName:@"successLogin" object:nil];

The class A is having one button "Validate" which calls class B's method and validate the user id and password entered by the user. it notify the observer if login is successful and observer is then Adding the login info to the db. The application allows to update db for 5 different login. (userid & password)

it works if I enter 1st record. when I add one more login info, the notification alert comes for twice. When I add one more it comes for thrice and so on. Also it updates db with the value of array which was at the time of 1st record addition

but when I enter 1st record and quit the application (by removing the application from minimize list of iPhone as well as Simulator) and again run it and tries to add second record. It adds it correctly. so for 5 additions I have to repeat the above cycle which is of course not conveniently for user.

Please help me to drag me out from this problem.


After trying lots of way, I finally got the solution.
Following is the solution which I want to share with you.

My Class A was having a button "Add New", which calls Class B for connection and authentication of id and password. The class B, based on the output (success or failure) posts the notification, which in terms should be handle by Class A.

I wrote the remove observer in dealloc method, which was actually causing the problem because the method was not at all being called.

and hence I shifted the code in my handler event method. now my method does look like this

`

- (void) successLogin: (NSNotification * ) notification
{  
   ... <some code of writing data to db using **array**>  
   [self showSuccessAlert]; // it is showing UIAlert  

[[NSNotificationCenter defaultCenter] removeObserver: self];  
}  

`

Also, I shifted the code of addObserver from init to AddNewButtonAction. So the things started working correctly.


Could you possibly share more of the code? From what you have posted, perhaps the notification is happening because you are initializing more than one instance of Class A, which is subscribing for additional notifications? It is difficult to help without seeing how you are defining and accessing the global array and Class A


Have you considered implementing this with the delegation pattern?

A could be made the delegate of B, and B can call whichever method you'd like on its delegate.


You just to remove observer of NSNotification's object. Because it may be possible that your init method will be called multiple times. so class it self adding that NSNotification multiple times.

so remove observer [[NSNotificationCenter defaultCenter] removeObserver: self]; after notifying Notification. self indicates NSNotification.

Hope this will help you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜