So I'm using a SIO socket singleton to create global variables that are received while the app is in use. I have the user enter a VC, upon entering the VC the socket is initialized and should begin receiving data from the socket. I then use NSNotificationCenter to post notifications throughout the app for when new data is emitted to the socket. Here is how I'm going about doing that.
- (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) {
NSArray *tracks = [args objectAtIndex:0];
self.setListTracks = tracks;
NSDictionary *tracksInfo = [NSDictionary dictionaryWithObjectsAndKeys:tracks,@"tracksArray", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"qUpdateB"
object:self
userInfo:tracksInfo];
}];
}
}
The socket works and is running. Upon the "initialize" callback, I try to push to a new VC with this code.
-(void)receiveInitializeNotification:(NSNotification *)notificaiton
{
dispatch_async(dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"toSetListRoomVC" sender:self];
});
}
And this works fine. (However, sometimes the segue is performed twice quickly). Now this is where the problem comes. Once the segue is performed, the new VC "setListVC" does not receive notification from "qUpdateB". SOMETIMES the setListVC's tableView will be populated with the tracks array received from the "q_update_B" callback. Am I threading poorly? Only sometimes do the notifications get recieved in setListVC.
Aucun commentaire:
Enregistrer un commentaire