Objective c ios,如何向单独的VC发送异步通知?

Objective c ios,如何向单独的VC发送异步通知?,objective-c,sockets,asynchronous,Objective C,Sockets,Asynchronous,现在,我正在使用NSNotificationCenter将同步通知从套接字单例发送到视图控制器。然而,这造成了一些问题。当ViewDid出现时,我的观察者没有收到应该收到的通知。如何异步执行此操作?通过在VC的viewDidLoad中发布通知,我设法让VC从我的套接字中填充数据,但这似乎不是正确的做法 我的应用程序是如何工作的,我向套接字发送数据,套接字发出一个称为“初始化”的回调,收到这个通知后,我推送到一个新的VC。这可能是造成问题的原因吗 -(void)receiveInitializeN

现在,我正在使用NSNotificationCenter将同步通知从套接字单例发送到视图控制器。然而,这造成了一些问题。当ViewDid出现时,我的观察者没有收到应该收到的通知。如何异步执行此操作?通过在VC的viewDidLoad中发布通知,我设法让VC从我的套接字中填充数据,但这似乎不是正确的做法

我的应用程序是如何工作的,我向套接字发送数据,套接字发出一个称为“初始化”的回调,收到这个通知后,我推送到一个新的VC。这可能是造成问题的原因吗

-(void)receiveInitializeNotification:(NSNotification *)notificaiton
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self performSegueWithIdentifier:@"toSetListRoomVC" sender:self];
    });
}
当前代码: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] ;

        }];
接收“初始化”通知

-(void)receiveInitializeNotification:(NSNotification *)notificaiton
{
        [self performSegueWithIdentifier:@"toSetListRoomVC" sender:self];
}
在SetListVC中接收qUpdateB

- (void)receiveUpdateBNotification:(NSNotification *)notification
{
    NSLog(@"Recieved update B");
    NSArray *recievedtracks = [[SocketKeeperSingleton sharedInstance]setListTracks];
    self.tracks = recievedtracks;
    [self.tableView reloadData];

}
我的“qUpdateB已被提名”被邀请加入新的VC。 但是,新VC中未收到通知。 如果我加上

[[NSNotificationCenter defaultCenter] postNotificationName:@"currentArtistB" object:nil];

对我的SetlistVC和观察者来说,它将按预期工作,但这似乎不正确

套接字正在网络线程上工作,不是吗?如果您在该线程中发布通知,则接收方将在网络线程上获得通知。但是,您只能在主线程上操作UI。因此,您真正需要的是在主线程上发布通知,这样您的视图控制器就可以在主线程上接收通知

[self performSelectorOnMainThread:@selector(postNotification) withObject:nil waitUntilDone:YES] ;

- (void)postNotification {
  [[NSNotificationCenter defaultCenter] postNotificationName:@"yourEventName" object:self] ;
}

我非常怀疑您是否真的需要异步方法,但是您是否看过
dispatch\u async
方法?我更新了我的问题,提供了更多详细信息,您可能是对的。这可能是由于我如何推动新的风险投资,这是造成问题。想法@CanPoyrazoğlu您应该确保您的视图控制器在发布“初始化”之前已经注册了通知。是的,这就是问题所在。我会认为你的答案是正确的@实际上,它现在可以工作了,但是有5-10秒的延迟。现在是2-3秒的延迟。嗯,也许是服务器端的问题。但这绝对是答案。谢谢。@Andy除非主线程忙于其他事情,否则对我来说这是不可能的,你应该避免这样做。我如何才能看到主线程是否忙于其他事情?我不知道为什么会这样。现在它已经完全停止工作了。代码完全相同。@Andy我认为问题来自服务器,请确保您已收到服务器的响应。