2013-02-26 98 views
2

我有以下AcceptCallBack方法,并希望在方法运行时添加UIActivityIndi​​cator,因此[mvc performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:YES];invoke是使UI更改的方法。然后我有这条线[mvc performSelectorOnMainThread:@selector(hide) withObject:nil waitUntilDone:YES];删除UIActivityIndi​​cator。然而,似乎发生的是只有在AcceptCallBack执行完毕后调用才会被调用。 AcceptCallBackinvoke不在两个不同的线程上运行,因此允许它们同时运行?iOS线程和在主线程上进行UI更改

void AcceptCallBack(
       CFSocketRef socket, 
       CFSocketCallBackType type, 
       CFDataRef address, 
       const void *data, 
       void *info) 
{ 
NSLog(@"Start Receiving..."); 

MasterViewController* mvc = (__bridge MasterViewController*)info; 
[mvc performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:YES]; 

CFReadStreamRef readStream = NULL; 
CFWriteStreamRef writeStream = NULL; 
CFIndex bytes; 
UInt8 buffer[8192]; 
UInt8 * fileData; 
UInt8 recv_len = 0; 

/* The native socket, used for various operations */ 
CFSocketNativeHandle sock = *(CFSocketNativeHandle *) data; 

/* Create the read and write streams for the socket */ 
CFStreamCreatePairWithSocket(kCFAllocatorDefault, sock, 
          &readStream, &writeStream); 

if (!readStream || !writeStream) { 
    close(sock); 
    fprintf(stderr, "CFStreamCreatePairWithSocket() failed\n"); 
    return; 
} 

CFReadStreamOpen(readStream); 
CFWriteStreamOpen(writeStream); 


bool headerRead = false; 
int dataWritten = 0; 
NSMutableString* filename = NULL; 


NSMutableString * header = [[NSMutableString alloc] init]; 

while (true) { 
    memset(buffer, 0, sizeof(buffer)); 
    bytes = CFReadStreamRead(readStream, buffer, sizeof(buffer)); 

    recv_len += bytes; 

    if (bytes < 0) { 
     fprintf(stderr, "CFReadStreamRead() failed: %d\n", (int)bytes); 
     close(sock); 
     return; 
    } 
    if (bytes == 0) { 
     break; 
    } 

    if (!headerRead) { 
     for (int b=0; b<bytes; b++) { 
      if (buffer[b] == '\n') { 
       headerRead = true; 
       NSLog(@"Header is: %@", header); 

       NSArray *listItems = [header componentsSeparatedByString:@":"]; 
       filename = [[NSMutableString alloc] init]; 
       [filename appendString:[listItems objectAtIndex:2]]; 
       [filename replaceOccurrencesOfString:@"/" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [filename length])]; 
       fileData = (UInt8*)malloc(sizeof(UInt8) * [[listItems objectAtIndex:3] intValue]); 

       b++; 
       memcpy(fileData, buffer + b, bytes - b); 
       dataWritten = bytes - b; 

       break; 
      } else { 
       [header appendFormat:@"%c", buffer[b]]; 
      } 

     } 
    } else { 
     memcpy(fileData + dataWritten, buffer, bytes); 
     dataWritten += bytes; 
    } 

} 



NSString* docFile = [NSTemporaryDirectory() stringByAppendingPathComponent:filename]; 

NSData * outputData = [[NSData alloc] initWithBytes:fileData length:dataWritten]; 
if ([outputData writeToFile:docFile atomically:false] == YES) { 
    NSLog(@"File received and successfully written out to file------------------------------"); 

    MasterViewController * thing = (__bridge MasterViewController*)info; 

    [thing restClient:NULL loadedFile:docFile]; 

    NSString *destDir = @"/Slide2Me/"; 
    [[thing restClient] uploadFile:filename toPath:destDir 
        withParentRev:nil fromPath:docFile]; 
    [mvc performSelectorOnMainThread:@selector(hide) withObject:nil waitUntilDone:YES]; 




} else { 
    NSLog(@"Failed to write received data to file"); 
} 

}

编辑 所以我落得这样做让我期望的结果是把所有上面的代码中dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)与我要让像在主线程上的变化夹心它所以...

void AcceptCallBack(
        CFSocketRef socket, 
        CFSocketCallBackType type, 
        CFDataRef address, 
        const void *data, 
        void *info) 
{ 


NSLog(@"Start Receiving..."); 

MasterViewController* mvc = (__bridge MasterViewController*)info; 

[mvc performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:YES]; 


dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ 
     [the code above]; 

}); 

[mvc performSelectorOnMainThread:@selector(hide) withObject:nil waitUntilDone:YES]; 
} 
+0

http://stackoverflow.com/questions/7290931/gcd-threads-program-flow-and-ui-updates/7291056#7291056 – bryanmac 2013-02-27 01:16:42

回答

6

您可以使用GCD使其成为多线程,并在指示器启动和停止时发出信号。 performSelector ...将在函数完成时执行.. 它并没有说你如何调用AcceptCallBack方法,我猜它已经在主线程中了。如果是,则需要在另一个线程中调用AcceptCallback并使用以下代码在主线程上执行“调用”方法。

dispatch_async(dispatch_get_main_queue(), ^{ 
    <do work here> 
}); 

http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW1

编辑:

我会做这样的

static dispatch_queue_t processing_queue; 
static dispatch_queue_t request_processing_queue() { 
    if (processing_queue == NULL) { 
     processing_queue = dispatch_queue_create("com.xxx.processing", 0); 
    } 
    return processing_queue; 
} 

void AcceptCallBack(
        CFSocketRef socket, 
        CFSocketCallBackType type, 
        CFDataRef address, 
        const void *data, 
        void *info) 
{ 

    __block MasterViewController* mvc = (__bridge MasterViewController*)info; 

    dispatch_async(processing_queue(), ^{ 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      [mvc invoke]; 
     }); 


     ..... < Do AcceptCallback Code Here > 


     dispatch_async(dispatch_get_main_queue(), ^{ 
      [mvc hide]; 
     }); 

    }); 

} 

警告:这仅仅是伪代码..

+0

使用GCD有相同的结果,这使我进一步研究并注意到AcceptCallBack实际上在主线程上运行。我还没有完全掌握这个套接字的东西......但AcceptCallBack被这样调用'TCPServer = CFSocketCreate(NULL,PF_INET,SOCK_STREAM,IPPROTO_TCP,kCFSocketAcceptCallBack,(CFSocketCallBack)AcceptCallBack,有没有办法使它运行在另一个线程(不是主要的)?? – user1135469 2013-02-27 01:35:38

+0

我已经把我在编辑中找到的答案,但是让我意识到'AcceptCallBack'在主线程中被调用的投票。 – user1135469 2013-02-27 12:15:59

+0

我编辑了我的答案伪代码解释.. – andykkt 2013-02-27 23:46:17