0
在我的應用程序中,我打開了WiFi上的服務器用於共享文件並將文件從網絡瀏覽器上傳到應用程序。 我有一個問題,它在主線程(UI線程)上工作,它會產生很多問題,有一種方法可以讓代碼在後臺工作嗎?CFReadStreamClientCallBack在後臺線程
這是代碼:
我使讀流用這種方法非阻塞:
if (!CFReadStreamSetClient (theReadStream,
kCFStreamEventHasBytesAvailable | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered | kCFStreamEventOpenCompleted,
(CFReadStreamClientCallBack)&MyCFReadStreamCallback,
(CFStreamClientContext *)(&theContext)))
{
NSError *err = [self getStreamError];
NSLog (@"AsyncSocket %p couldn't attach read stream to run-loop,", self);
NSLog (@"Error: %@", err);
if (errPtr) *errPtr = err;
return NO;
}
和它調用此方法:
static void MyCFReadStreamCallback (CFReadStreamRef stream, CFStreamEventType type, void *pInfo)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
AsyncSocket *socket = [[(AsyncSocket *)pInfo retain] autorelease];
[socket doCFReadStreamCallback:type forStream:stream];
[pool release];
}
這是源代碼,我使用: http://code.google.com/p/cocoaasyncsocket/