Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 与AsyncSocket结合使用时,NSNotification奇怪_Iphone_Nsnotifications - Fatal编程技术网

Iphone 与AsyncSocket结合使用时,NSNotification奇怪

Iphone 与AsyncSocket结合使用时,NSNotification奇怪,iphone,nsnotifications,Iphone,Nsnotifications,我正在使用AsyncSocket从iPhone应用程序连接到服务器。在从服务器接收数据的委托中,我发布了一个通知,通知tableView的委托在tableView上触发重新加载数据: - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData*)data withTag:(long)tag { [[NSNotificationCenter defaultCenter] postNotificationName:@"PEERSTATUS

我正在使用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上:

- (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无法识别。但是,当我在ApplicationIDFinishLaunching中执行相同操作时:

- (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的委托方法位于不同的线程中有关


提前感谢。

一种可能是您的
initWithNibName:bundle:
方法实际上没有被调用。如果在NIB中实例化视图控制器(而不是在代码中),那么它将调用
initWithCoder:


快速检查的方法是在
initWithNibName:bundle:

中放置断点,尝试将发送通知的方法放在其他方法中,并使用“performSelectorOnMainThread”调用它。很可能您的网络代码在后台线程中被调用,因此当通知发出时,它会通知同一线程上的表视图


除了主线程之外,您不能对任何对象进行UI调用。

我确实在initWithNibName:bundle:方法中设置了一个断点,并且通知注册确实运行了。此外,viewController确实收到了ApplicationIDFinishLaunching方法中发布的通知。真奇怪。你是说AsyncSocket不应该影响nsnotification的工作方式?AsyncSocket不应该影响通知。那么onSocket:didReadData:呢?您确定正在调用该方法吗?您可能永远不会启动初始的read…AsyncSocket在调用其读/写方法的线程的运行循环上调度其网络操作。而且,通知是在其发布的线程上发送的,因此除非线程被显式分离,否则通知方法应该发生在主线程上。但是,如果从应用程序委托调用了重载数据,但当它确实起作用时,通知方法不起作用,那么几乎可以肯定他在某个地方使用了后台线程。