ios - Object parameter in method postNotification of NSNotificationCenter -
in ios
application, posting nsnotification
, catching in 1 of uiview
in main thread. want pass information along notification. using userinfo
dictionary of nsnotification
that.
[[nsnotificationcenter defaultcenter] postnotificationname:@"notifyvaluecomputedfromjs" object:self userinfo:@{@"notificationkey":key,@"notificationvalue":value,@"notificationcolor":color,@"notificationtimestamp":time}];
key, value, color , time local variables contains value need pass. in uiview
adding observer notification , using notification.userinfo
these data
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(receivenotification:) name:@"notifyvaluecomputedfromjs" object:nil]; -(void)receivenotification:(nsnotification *)notification { if ([notification.userinfo valueforkey:@"notificationkey"]!=nil && [[notification.userinfo valueforkey:@"notificationkey"] isequaltostring:self.notificationkey] && [notification.userinfo valueforkey:@"notificationvalue"]!=nil) { [self updatelabelwithvalue:[notification.userinfo valueforkey:@"notificationvalue"]]; } }
the frequency in notification posted 4 times in 1 second. doing animations in main thread. problem facing here ui lagging. ui respond scroll events or touch events huge delay(i have faced delay of 1 2 seconds). after research came know nsdictionary
bulky , cause lag if used in main thread. there other way can pass data through nsnotification?
i have tried out way. have created custom nsobject
class save data want , passing object parameter of postnotification
method.
[[nsnotificationcenter defaultcenter] postnotificationname:@"notifyvaluecomputedfromjs" object:customdataobject userinfo:nil];
here customdataobject
instance of custom nsobject
class. know parameter meant sender of notification(usually self). wrong approach if sending custom object parameter?
as bobdave mentioned, key send notification on thread other main ui thread. can accomplished dispatch_async, or queue.
the typical pattern behavior sender:
-(void)senddatatoobserver { dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ [[nsnotificationcenter defaultcenter] postnotificationname:@"notifyvaluecomputedfromjs" object:customdataobject userinfo:userinfo:@{@"notificationkey":key,@"notificationvalue":value,@"notificationcolor":color,@"notificationtimestamp":time}]; }); }
and receiver (note: weak self because retain cycles):
-(void)addobserver { [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(receivenotification:) name:@"notifyvaluecomputedfromjs" object:nil]; } -(void)receivenotification:(nsnotification *)notification { if ([notification.userinfo valueforkey:@"notificationkey"]!=nil && [[notification.userinfo valueforkey:@"notificationkey"] isequaltostring:self.notificationkey] && [notification.userinfo valueforkey:@"notificationvalue"]!=nil) { __weak typeof (self) weakself = self; dispatch_async(dispatch_get_main_queue(), ^{ [weakself updatelabelwithvalue:[notification.userinfo valueforkey:@"notificationvalue"]]; }); } }
Comments
Post a Comment