0

从 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

4

1 回答 1

1

CFNetwork 是基于运行循环的。要实现您的要求,您可以使用CFRunLoopAPI。像这样:

// ...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);
});

希望有帮助。

于 2013-09-12T18:36:59.120 回答