GCDAsyncSocket “didReadDataWithTag” Never called with NSOperation subclass

最后都变了- 提交于 2019-12-11 03:28:32

问题


In a "getMyFile" method of some XIB file.
I am creating a object of class "A"(subclass of NSOperation) and adding it to a "myFileQueue"(object of NSOperationQueue).

myFileQueue.MaxConcurrentOperationCount = 1;

Problem : didReadDataWithTag" delegate NEVER called in any case.

#import "GCDAsyncSocket.h" 
//and all other required classes are imported correctly

//Class : A
@interface  A : NSOperation
{
  {
   GCDAsyncSocket* socket;
dispatch_queue_t dQueue;

BOOL isWorkDone;
  }
}
@implementation  A {

   -main(){
      @autoreleasepool {

    isWorkDone = NO;
    dQueue = dispatch_queue_create(@"MyDQueue", NULL);
    socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dQueue];

    //NOTE: Even after setting delegateQueue: dispatch_get_main_queue()
    //didReadDataWithTag delegate never called

    NSError *err = nil;
    if (![socket connectToHost:@"192.168.1.142" onPort:12345 error:&err]) // Asynchronous!
    {
    // If there was an error, it's likely something like "already connected" or "no delegate set"
    NSLog(@"I goofed: %@", err);
    }

    do{
        [NSThread sleep:0.2];
        }while(!isWorkDone)


    }
}

   - (void)socket:(GCDAsyncSocket *)sender didConnectToHost:(NSString *)host port:(UInt16)port
   {
NSString* myString= @"testing";
NSData* data=[myString dataUsingEncoding: [NSString defaultCStringEncoding] ];
[socket writeData:data withTimeout:-1 tag:1];
   }
  - (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
  {
[socket readDataToData:[GCDAsyncSocket ZeroData] withTimeout:-1 tag:TAG_RESPONSE_HEADER];
  } 
- (void)socket:(GCDAsyncSocket *)sender didReadData:(NSData *)data withTag:(long)tag
 {
 NSLog(@"didReadDataWithTag called...."); //this method never called in any case

 isWorkDone = YES;
 }
}

来源:https://stackoverflow.com/questions/17631177/gcdasyncsocket-didreaddatawithtag-never-called-with-nsoperation-subclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!