当我执行以下操作时:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, NULL), ^{
create NSURLRequest;
create NSURLConnectionDelegate;
create NSURLConnection;
start NSURLConnection;
});
委托的方法永远不会被调用。
但当我这样做时
dispatch_async(dispatch_get_main_queue(), ^{
create NSURLRequest;
create NSURLConnectionDelegate;
create NSURLConnection;
start NSURLConnection;
});
他们确实会接到电话。为什么?
UPD
http://developer.apple.com/library/ios/#qa/qa1712/_index.html http://developer.apple.com/library/ios/#qa/qa1712/_index.html
现在我做
创建 NSURLConnection;
启动 NSURLConnection;
在主线程上。
在第一种情况下,该队列将被某个工作线程耗尽,该线程很可能不会运行运行循环。
在第二种情况下,队列由应用程序的主线程清空,该主线程将运行一个运行循环。因此委托方法被安排在该运行循环上。
希望 Apple 能够尽快为此提供基于队列和块的 API。同时,你可能会想ASIHTTP请求 http://allseeing-i.com/ASIHTTPRequest/,它允许您在连接完成(或失败)时将块提交到 NSOperationQueue。
或者您可以显式配置 NSURLConnection 以使用主线程的运行循环(或您知道将存在足够长的时间的其他特定运行循环)。请参阅 -[NSURLConnection –scheduleInRunLoop:forMode:]
希望有帮助吗?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)