2

在一些 XIB 文件的“getMyFile”方法中。
我正在创建“A”类(NSOperation 的子类)的对象并将其添加到“myFileQueue”(NSOperationQueue 的对象)中。

myFileQueue.MaxConcurrentOperationCount = 1;

问题:在任何情况下都不会调用 didReadDataWithTag" 委托。

#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;
 }
}
4

0 回答 0