Ios NSNotification未从调度异步内部激发
我有以下代码: 在会话completionHandler中,我有一个dispatch_async,其中包含对NSNotificationCenter post通知方法的调用。但是通知从来没有被激发过任何想法Ios NSNotification未从调度异步内部激发,ios,Ios,我有以下代码: 在会话completionHandler中,我有一个dispatch_async,其中包含对NSNotificationCenter post通知方法的调用。但是通知从来没有被激发过任何想法 NSURLSession *session = [NSURLSession sharedSession]; [[session dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData *data, NSURL
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// populating appDelegate.statusItems variable /////
.......
dispatch_async(dispatch_get_main_queue()
, ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataReceived" object:nil userInfo:@{@"StatusItems":appDelegate.statusItems}];
[self performSegueWithIdentifier:@"DashboardSegue" sender:nil];
});
}]resume];
在LoginViewController(代码在上面)中,我还为侦听器注册了通知,但从未调用它们:
-(void) setupNotifications
{
ViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
[viewController registerNotifications];
}
-(void) registerNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(populateList:) name:@"DataReceived" object:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void) populateList:(NSNotification *) notification
{
NSDictionary *dict = notification.userInfo;
_statusItems = [dict valueForKey:@"StatusItems"];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
是否调用registerNotifications方法?您是否必须使用[self registerNotifications]而不是[viewController registerNotifications]仪表板Segue类接收通知。可能会在创建此视图之前发送通知。