在CFNetwork线程中,我希望在主队列上执行一些处理,并异步获取结果。现在,我正在将使用dispatch_get_current_queue获得的结果分派到队列中,以获取结果。
dispatch_queue_t baseQueue = dispatch_get_current_queue();
dispatch_async(dispatch_get_main_queue(), ^{
NSString* content = [self processSomething];
dispatch_async(baseQueue, ^{
[self sendResults:result];
});
});不幸的是,dispatch_get_current_queue已被弃用。如何在不使用dispatch_get_current_queue的情况下实现相同的功能?
发布于 2013-09-13 02:36:59
CFNetwork是基于运行循环的。要实现您所要求的,您可以使用CFRunLoop应用编程接口。如下所示:
// ...from some code called by CFNetwork on its run loop
CFRunLoop cfNetworkRunLoop = CFRunLoopGetCurrent();
dispatch_async(dispatch_get_main_queue(), ^{
// On the main thread...
NSString* content = [self processSomething];
CFRunLoopPerformBlock(cfNetworkRunLoop, kCFRunLoopCommonModes, ^{
// Back on CFNetwork's run loop
[self sendResults:result];
});
// Necessary for your block to run right away, otherwise it might
// be delayed (until something else wakes up the run loop.)
CFRunLoopWakeUp(cfNetworkRunLoop);
});希望这能有所帮助。
https://stackoverflow.com/questions/18696050
复制相似问题