NSNotification 与 AsyncSocket 结合使用时的奇怪之处

发布于 2024-07-25 20:20:50 字数 1772 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剩余の解释 2024-08-01 20:20:50

一种可能性是您的 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 calls initWithCoder: instead.

A quick way to check is to put a breakpoint in initWithNibName:bundle:.

放血 2024-08-01 20:20:50

尝试将发送通知的方法放在不同的方法中,并使用“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文