如题,我在子线程main函数里面开了一个socket,main函数走完socket的代理完全没有进行回调,求解这是什么情况。

解决方案 »

  1.   

    设置委托了,我这样写的
    @implementation ZBOperation@synthesize asyncSocket;
    @synthesize host;- (id)initWithhost:(NSString *)hostip
    {
        if (![super init]) {
            return [super init];
        }
        self.host = hostip;
        return self;
    }- (void)main
    {
        self.asyncSocket = [[AsyncSocket alloc]initWithDelegate:self];
        [self.asyncSocket connectToHost:self.host onPort:5867 withTimeout:-1 error:nil];
        NSLog(@"%@",self.host);
    }- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
    {
        NSLog(@"onSocket:%p willDisconnectWithError:%@", sock, err);
        
    }- (void)onSocketDidDisconnect:(AsyncSocket *)sock
    {
        NSLog(@"onSocketDidDisconnect:%p", sock);
    }- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)hostip port:(UInt16)port
    {
        NSLog(@"onSocket:%p didConnectToHost:%@ port:%hu", sock, hostip, port);
        [sock readDataWithTimeout:-1 tag:0];
    }- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
    {
        NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"===%@",aStr);
    }
      

  2.   

    找到原因了,NSOperation这个东西再start以后执行完任务它自己就把线程关闭了,所以是无法调用到回调的。据说再线程发出异步请求后要启动一个runloop,保证线程不退出,接受到delegate。
      

  3.   

    子线程作为委托的情况下要手动配置runloop,否则不会响应回调。
      

  4.   

    子线程作为委托的情况下要手动配置runloop,否则不会响应回调。