ios - 使用 IOS 4 异步调用方法

标签 ios asynchronous ios4

我想异步调用一个方法。这是一种从服务器获取 HTML 并将其设置为 UIWebView 的方法:

NSString *htmlTest = [BackendProxy getContent];
[webView loadHTMLString:htmlTest baseURL: nil];
[webView setUserInteractionEnabled:YES];

我想在数据获取期间在 UIWebView 中启动一个事件指示器,因此我需要异步调用 getContent。我怎样才能做到这一点?

最佳答案

这是 GCD(Apple 的新(ish)并发 API)的一个很好的用例。

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^ {
    // Background work here
    NSLog(@"Finished work in background");
    dispatch_async(dispatch_get_main_queue(), ^ {
        NSLog(@"Back on main thread");
    });
});

这是关于 dispatch queues 的文档

关于ios - 使用 IOS 4 异步调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8854100/

相关文章:

javascript - setTimeout 在 IE11 上无法按预期工作

iphone - 如何获得带有“下一步”、“上一步”和“完成”按钮的键盘?

node.js - 多个异步请求

iphone - CL位置距位置的距离

iphone - UIModalTransitionStylePartialCurl 与 UITabBarController

ios - Bonjour 如何在 IOS 上运行?

ios - Swift: 将 UIView 设置为 UIBarButton/UIButton 的背景

ios - ITMS-90535 无法发布 libgdx iOS 应用程序

iphone - UILabel 无法更新标签文本

file - 如何使用 tokio::fs 复制文件