NSNotification 与 AsyncSocket 结合使用时的奇怪之处
我正在使用 AsyncSocket 从我的 iPhone 应用程序连接到服务器。 在从服务器接收数据的委托中,我发布了一条通知,告诉 tableView 的委托在 tableView:
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData*)data withTag:(long)tag {
[[NSNotificationCenter defaultCenter] postNotificationName:@"PEERSTATUSCHANGED" object:self];
[sock readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:0];
}
和 viewController: 上触发 reloadData:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(peerStatusDidChange:) name:@"PEERSTATUSCHANGED" object:nil];
}
return self;
}
- (void)peerStatusDidChange:(NSNotification *)notification {
NSLog(@"NOTIFICATION RECEIVED");
}
现在,这根本不起作用。 通知已发出,但 ViewController 无法识别。 但是,当我在 applicationDidFinishLaunching: 中执行相同的操作时,
- (void)applicationDidFinishLaunching:(UIApplication *)application {
protocol = [[XBBProtocol alloc] init];
SourceListViewController *sourceListVC = [[[SourceListViewController alloc] initWithNibName:@"SourceListViewController" bundle:nil] autorelease];
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:sourceListVC] autorelease];
[[NSNotificationCenter defaultCenter] postNotificationName:@"PEERSTATUSCHANGED" object:self];
[protocol connectToServer];
// Override point for customization after application launch
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
我收到了 viewController 中收到的通知。
有人知道为什么吗? 它与 AsyncSocket 位于不同线程中的委托方法有关吗?
提前致谢。
I'm using AsyncSocket to connect to a server from my iPhone App. In the delegate that received data from the server, I post a notification that would tell the tableView's delegate to trigger a reloadData on the tableView:
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData*)data withTag:(long)tag {
[[NSNotificationCenter defaultCenter] postNotificationName:@"PEERSTATUSCHANGED" object:self];
[sock readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:0];
}
and on the viewController:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(peerStatusDidChange:) name:@"PEERSTATUSCHANGED" object:nil];
}
return self;
}
- (void)peerStatusDidChange:(NSNotification *)notification {
NSLog(@"NOTIFICATION RECEIVED");
}
Now, this doesn't work at all. The notification is posed but not recognized by the ViewController. However, when I do the same thing in applicationDidFinishLaunching:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
protocol = [[XBBProtocol alloc] init];
SourceListViewController *sourceListVC = [[[SourceListViewController alloc] initWithNibName:@"SourceListViewController" bundle:nil] autorelease];
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:sourceListVC] autorelease];
[[NSNotificationCenter defaultCenter] postNotificationName:@"PEERSTATUSCHANGED" object:self];
[protocol connectToServer];
// Override point for customization after application launch
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
I got the notification received in viewController.
Anyone knows why? does it have something to do with delegate methods of AsyncSocket being in different thread?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种可能性是您的
initWithNibName:bundle:
方法实际上并未被调用。 如果您在 NIB 中(而不是在代码中)实例化视图控制器,那么它会调用initWithCoder:
。一种快速检查方法是在
initWithNibName:bundle:
中放置一个断点。One possibility is that your
initWithNibName:bundle:
method is not actually being called. If you instantiate the view controller in a NIB (rather than in code), then it callsinitWithCoder:
instead.A quick way to check is to put a breakpoint in
initWithNibName:bundle:
.尝试将发送通知的方法放在不同的方法中,并使用“performSelectorOnMainThread”调用它。 您的网络代码很可能在后台线程中被调用,因此当通知触发时,它会通知同一线程上的表视图...
您无法在除主线程之外的任何内容上进行 UI 调用。
Try putting the method that sends the notification in a different method, and call it with "performSelectorOnMainThread". It's very likely your network code is getting called in a background thread and thus when the notification fires, it informs the table view on the same thread...
You can't make UI calls on anything but the main thread.