ios - Post a notification within a NSOperationQueue -


i need nsnotificationcenter. have class called sensor , class called sensormanager. i'd send notification sensor sensormanager. in sensormanager write code:

[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(receivetestnotification:) name:@"testnotification" object:nil]; 

and, clearly, have function:

- (void) receivetestnotification:(nsnotification *) notification {                     if ([[notification name] isequaltostring:@"testnotification"])         nslog (@"successfully received test notification!"); } 

in sensor class have function starts sensor:

-(void)worksensor{                        self.motionmanager.accelerometerupdateinterval = 0.05;          nsoperationqueue *queue = [[nsoperationqueue alloc]init];         [self.motionmanager startaccelerometerupdatestoqueue:queue withhandler:^(cmaccelerometerdata *data, nserror *error) {                             [[nsnotificationcenter defaultcenter] postnotificationname:@"testnotification" object:self];              nslog(@"udapte iphone acc data!");         }];     }  } 

unfortunately, sensormanager doesn't catch notification. strange thing (from point of view) if move notification code outside nsoperationqueue block, works great (see code below):

-(void)worksensor{                 //now notification here. outside block.             [[nsnotificationcenter defaultcenter] postnotificationname:@"testnotification" object:self];              self.motionmanager.accelerometerupdateinterval = 0.05;              nsoperationqueue *queue = [[nsoperationqueue alloc]init];             [self.motionmanager startaccelerometerupdatestoqueue:queue withhandler:^(cmaccelerometerdata *data, nserror *error) {                                   nslog(@"udapte iphone acc data!");             }];         }      } 

how can put notification sender inside nsoperationqueue block? thank you!

notifications should posted on main thread, should this:

[self performselectoronmainthread:@selector(sendnotification) withobject:nil waituntildone:yes]; 

which call function on main thread. define notification sending function:

- (void)sendnotification {   [[nsnotificationcenter defaultcenter] postnotificationname:@"myeventname" object:self];  } 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -