首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CFNetwork线程的回调

CFNetwork线程的回调
EN

Stack Overflow用户
提问于 2013-09-09 18:19:02
回答 1查看 218关注 0票数 0

在CFNetwork线程中,我希望在主队列上执行一些处理,并异步获取结果。现在,我正在将使用dispatch_get_current_queue获得的结果分派到队列中,以获取结果。

代码语言:javascript
复制
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的情况下实现相同的功能?

EN

回答 1

Stack Overflow用户

发布于 2013-09-13 02:36:59

CFNetwork是基于运行循环的。要实现您所要求的,您可以使用CFRunLoop应用编程接口。如下所示:

代码语言:javascript
复制
// ...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);
});

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18696050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档