Reputation: 770
When I receive data from a socket and pass the data to another VC via NSNotificationCenter, the passed object always logs (null), despite the object being present in the other class.
there is where I pass the data through.
UPDATED:
-(void) initSIOSocket {
[SIOSocket socketWithHost:@"http://192.168.1.4:8080" response:^(SIOSocket *socket) {
self.socket = socket;
NSLog(@"%@ from initSIOSocket", self.socket);
[self.socket on:@"q_update_B" callback:^(NSArray *args) {
NSArray *tracks = [args objectAtIndex:0];
[[NSNotificationCenter defaultCenter] postNotificationName:@"qUpdateB" object:nil userInfo:[NSDictionary dictionaryWithObject:tracks forKey:@"tracksData"]];
}];
..
- (void)receiveUpdateBNotification:(NSNotification *)notification
{
// Do parse respone data method and update yourTableViewData
NSArray *tracks = [[notification userInfo] objectForKey:@"tracksData"];
NSLog(@"%@", tracks);
self.tracks = tracks;
[self.tableView reloadData];
}
Console is STILL logging as (null) object. The notification is successful, no data is sent.
Upvotes: 0
Views: 1041
Reputation: 770
I needed to use my Singleton to pass the data using the NSNotificationCenter, like so.
-(void) initSIOSocket {
[SIOSocket socketWithHost:@"http://192.168.1.4:8080" response:^(SIOSocket *socket) {
self.socket = socket;
NSLog(@"%@ from initSIOSocket", self.socket);
[self.socket on:@"q_update_B" callback:^(NSArray *args) {
NSArray *tracks = [args objectAtIndex:0];
self.setListTracks = tracks;
[[NSNotificationCenter defaultCenter] postNotificationName:@"qUpdateB" object:nil];
}];
}];
}
..
- (void)receiveUpdateBNotification:(NSNotification *)notification
{
if ([[notification name] isEqualToString:@"qUpdateB"])
NSLog (@"Successfully received the test notification!");
// Do parse respone data method and update yourTableViewData
NSArray *tracks = [[SocketKeeperSingleton sharedInstance]setListTracks];
self.tracks = tracks;
[self.tableView reloadData];
}
Upvotes: -1
Reputation: 107141
For passing data using NSNotification
you need to use the userInfo
dictionary.
Post it like:
[[NSNotificationCenter defaultCenter] postNotificationName:@"qUpdateB" object:nil userInfo:[NSDictionary dictionaryWithObject:tracks forKey:@"MyData"]];
And retrieve it using:
- (void)receiveUpdateBNotification:(NSNotification *)notification
{
self.tracks = [[notification userInfo] objectForKey:@"MyData"];
[self.tableView reloadData];
}
Object property is not intended for passing data.
object
The object associated with the notification. (read-only) Declaration
@property(readonly, retain) id object Discussion;
This is often the object that posted this notification. It may be nil.
Typically you use this method to find out what object a notification applies to when you receive a notification.
For example, suppose you’ve registered an object to receive the message handlePortDeath: when the “PortInvalid” notification is posted to the notification center and that handlePortDeath: needs to access the object monitoring the port that is now invalid. handlePortDeath: can retrieve that object as shown here:
- (void)handlePortDeath:(NSNotification *)notification { ... [self reclaimResourcesForPort:notification.object]; ... }
Upvotes: 2