[英]NSURLConnection and sendAsynchronousRequest:queue:completionHandler: - does the completion block run in the main thread
[英]What is the main difference between NSURLConnection connectionWithRequest:delegate and sendAsynchronousRequest:queue:completionHandler?
我想知道两个NSURLConnection协议或方法(或实际调用它们)的区别。
我是Objective-C编程的新手,我正在尝试创建一个从网站加载数据的iOS应用程序。 我注意到在我看过/读过的教程中,当他们使用connectionWithRequest时,他们有其他方法在我看来是connectionWithRequest正常工作所需的方法。 一个教程就是这个 。 这些方法是:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
仅举两个例子。 关于这一点,我有另一个问题:当调用或使用NSURLConnection connectionWithRequest:委托时,是否会自动调用这些方法?
我提到的两个NSURLConnection协议之间究竟有什么区别? 哪个更好用?
我找到了一个很好的链接,让你了解NSURLConnection及其委托方法。
http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/
主要区别在于不同类型的回调:
Method -connectionWithRequest:delegate:
使用委托,你必须实现didReceiveResponse
, didReceiveData
和其他才能使它有用。 这更复杂 ,但允许您处理安全身份验证,进度跟踪和类似的事情。
Method +sendAsynchronousRequest:queue:completionHandler:
使用块完成,与委托相比,这更容易 。 它处理接收响应和附加正文数据,并简单地为您提供结果。
脚注: iOS 7引入了全新的NSURLSessions
URL加载机制,可以代替NSURLConnection
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.