iphone - NSNotificationCenter receive event method is executing multiple times -


i java developer , assigned task on objective c (iphone).

i have started coding in language. while implementing nsnotificationcenter having strange problem.

it difficult explain problem.

my class having 1 global variable named array of type nsmutablearray pointer. class's init method looks 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>    } 

recieve event method looks like

- (void) successlogin: (nsnotification * ) notification {      ... <some code of writing data db using **array**>      [self showsuccessalert]; // showing uialert } 

the sendevent method (of other class b) having code

[[nsnotificationcenter defaultcenter] postnotificationname:@"successlogin" object:nil]; 

the class having 1 button "validate" calls class b's method , validate user id , password entered user. notify observer if login successful , observer adding login info db. application allows update db 5 different login. (userid & password)

it works if enter 1st record. when add 1 more login info, notification alert comes twice. when add 1 more comes thrice , on. updates db value of array @ time of 1st record addition

but when enter 1st record , quit application (by removing application minimize list of iphone simulator) , again run , tries add second record. adds correctly. 5 additions have repeat above cycle of course not conveniently user.

please me drag me out problem.

after trying lots of way, got solution.
following solution want share you.

my class having button "add new", calls class b connection , authentication of id , password. class b, based on output (success or failure) posts notification, in terms should handle class a.

i wrote remove observer in dealloc method, causing problem because method not @ being called.

and hence shifted code in handler event method. method

`

- (void) successlogin: (nsnotification * ) notification {      ... <some code of writing data db using **array**>      [self showsuccessalert]; // showing uialert    [[nsnotificationcenter defaultcenter] removeobserver: self];   }   

`

also, shifted code of addobserver init addnewbuttonaction. things started working correctly.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -