ios,如何向单独的 VC 发送异步通知?
ios, how do I send an asynchronous notification to a seperate VC?
现在我正在使用 NSNotificationCenter 将同步通知从我的套接字单例发送到视图控制器。然而,这会导致问题。在 viewDidAppear 上,我的观察者没有在应该收到通知的时候收到通知。我怎样才能异步执行此操作?通过在我的 VC 的 viewDidLoad 中发布通知,我设法让 VC 填充了来自我的套接字的数据,但这似乎不是正确的做法。
我的应用程序如何工作,我向套接字发送数据,套接字返回一个名为 "initialize" 的回调,根据此通知,我推送到一个新的 VC。这可能是导致问题的原因吗?
-(void)receiveInitializeNotification:(NSNotification *)notificaiton
{
dispatch_async(dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"toSetListRoomVC" sender:self];
});
}
当前 CODE:Socket
- (void)startSocketWithHost:(NSString *)host;{
[SIOSocket socketWithHost:host response:^(SIOSocket *socket) {
self.socket = socket;
//Send a message to RoomCode controler to notify the reciever that the user has enetered a correct code and can enter the specific setList room.
[self.socket on:@"initialize" callback:^(NSArray *args) {
NSDictionary *socketIdDict = [args objectAtIndex:0];
NSString *socketID = [socketIdDict objectForKey:@"socket"];
self.socketID = socketID;
[[NSNotificationCenter defaultCenter] postNotificationName:@"initialize" object:nil];
}];
//on Callback for events related to updates with the song queue.
[self.socket on:@"q_update_B" callback:^(NSArray *args) {
NSLog(@"qUpdateB has been emitted");
NSArray *tracks = [args objectAtIndex:0];
self.setListTracks = tracks;
[self performSelectorOnMainThread:@selector(postQUpdateBNotification) withObject:nil waitUntilDone:YES] ;
}];
[self.socket on:@"current_artist_B" callback:^(NSArray *args) {
self.currentArtist = [args objectAtIndex:0];
[self performSelectorOnMainThread:@selector(postCurrentArtistBNotification) withObject:nil waitUntilDone:YES] ;
}];
收到 "initialize" 通知。
-(void)receiveInitializeNotification:(NSNotification *)notificaiton
{
[self performSegueWithIdentifier:@"toSetListRoomVC" sender:self];
}
在 SetList 中接收 qUpdateBVC
- (void)receiveUpdateBNotification:(NSNotification *)notification
{
NSLog(@"Recieved update B");
NSArray *recievedtracks = [[SocketKeeperSingleton sharedInstance]setListTracks];
self.tracks = recievedtracks;
[self.tableView reloadData];
}
我的 "qUpdateB has been emmited" 被要求转到新的 VC。
但是,在新 VC 中未收到通知。
如果我添加
[[NSNotificationCenter defaultCenter] postNotificationName:@"currentArtistB" object:nil];
到我的 SetlistVC 和观察者一起然后它将按预期工作,但这似乎不正确。
套接字正在网络线程上工作,不是吗?如果您 post 该线程中的通知,接收者将在网络线程中收到通知。但是你只能在主线程上操作 UI 。所以你真正需要的是 post 主线程上的通知,这样你的视图控制器就可以在主线程上接收通知。
[self performSelectorOnMainThread:@selector(postNotification) withObject:nil waitUntilDone:YES] ;
- (void)postNotification {
[[NSNotificationCenter defaultCenter] postNotificationName:@"yourEventName" object:self] ;
}
现在我正在使用 NSNotificationCenter 将同步通知从我的套接字单例发送到视图控制器。然而,这会导致问题。在 viewDidAppear 上,我的观察者没有在应该收到通知的时候收到通知。我怎样才能异步执行此操作?通过在我的 VC 的 viewDidLoad 中发布通知,我设法让 VC 填充了来自我的套接字的数据,但这似乎不是正确的做法。
我的应用程序如何工作,我向套接字发送数据,套接字返回一个名为 "initialize" 的回调,根据此通知,我推送到一个新的 VC。这可能是导致问题的原因吗?
-(void)receiveInitializeNotification:(NSNotification *)notificaiton
{
dispatch_async(dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"toSetListRoomVC" sender:self];
});
}
当前 CODE:Socket
- (void)startSocketWithHost:(NSString *)host;{
[SIOSocket socketWithHost:host response:^(SIOSocket *socket) {
self.socket = socket;
//Send a message to RoomCode controler to notify the reciever that the user has enetered a correct code and can enter the specific setList room.
[self.socket on:@"initialize" callback:^(NSArray *args) {
NSDictionary *socketIdDict = [args objectAtIndex:0];
NSString *socketID = [socketIdDict objectForKey:@"socket"];
self.socketID = socketID;
[[NSNotificationCenter defaultCenter] postNotificationName:@"initialize" object:nil];
}];
//on Callback for events related to updates with the song queue.
[self.socket on:@"q_update_B" callback:^(NSArray *args) {
NSLog(@"qUpdateB has been emitted");
NSArray *tracks = [args objectAtIndex:0];
self.setListTracks = tracks;
[self performSelectorOnMainThread:@selector(postQUpdateBNotification) withObject:nil waitUntilDone:YES] ;
}];
[self.socket on:@"current_artist_B" callback:^(NSArray *args) {
self.currentArtist = [args objectAtIndex:0];
[self performSelectorOnMainThread:@selector(postCurrentArtistBNotification) withObject:nil waitUntilDone:YES] ;
}];
收到 "initialize" 通知。
-(void)receiveInitializeNotification:(NSNotification *)notificaiton
{
[self performSegueWithIdentifier:@"toSetListRoomVC" sender:self];
}
在 SetList 中接收 qUpdateBVC
- (void)receiveUpdateBNotification:(NSNotification *)notification
{
NSLog(@"Recieved update B");
NSArray *recievedtracks = [[SocketKeeperSingleton sharedInstance]setListTracks];
self.tracks = recievedtracks;
[self.tableView reloadData];
}
我的 "qUpdateB has been emmited" 被要求转到新的 VC。 但是,在新 VC 中未收到通知。 如果我添加
[[NSNotificationCenter defaultCenter] postNotificationName:@"currentArtistB" object:nil];
到我的 SetlistVC 和观察者一起然后它将按预期工作,但这似乎不正确。
套接字正在网络线程上工作,不是吗?如果您 post 该线程中的通知,接收者将在网络线程中收到通知。但是你只能在主线程上操作 UI 。所以你真正需要的是 post 主线程上的通知,这样你的视图控制器就可以在主线程上接收通知。
[self performSelectorOnMainThread:@selector(postNotification) withObject:nil waitUntilDone:YES] ;
- (void)postNotification {
[[NSNotificationCenter defaultCenter] postNotificationName:@"yourEventName" object:self] ;
}