我试图写一个使用Swift的HTTP请求,我真的只能看到人们以某种方式(实际上没有那么多GCD)在iOS上执行HTTP请求。Swift - Grand Central Dispatch vs Blocks/Closures
如果我想在iOS中发出HTTP请求,最好的方法是什么:使用Grand Central Dispatch或者写一些闭包块?另外,我在哪里可以找到一些很好的例子。从我在网上阅读的内容来看,我很难看到一个人的好处。
目前,我有这样的代码:
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0), {()->() in
self.activityIndicator.startAnimating()
self.authenticateUser()
dispatch_async(dispatch_get_main_queue(), {
self.activityIndicator.stopAnimating()
})
})
从我在调试会话标签的处理器看,我可以看到它从主题X跳到主题Y(为的authenticateUser)和回到线程X.哪个好 - 这是我想要的(我确定)。
在Swift中编写的GCD或Blocks/Closures的任何很好的例子都是非常方便的。谢谢!
GCD使用闭包。不能在不使用闭包的情况下在Swift中使用GCD。无论如何,你看过[* URL加载系统编程指南*](https://developer.apple.com/library/ios/Documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html#//apple_ref/doc/uid/10000165i)? – 2014-09-25 21:20:41
看一看:https://medium.com/swift-programming/http-in-swift-693b3a7bf086。 – 2014-09-25 21:28:46
@MaximShoustin比使用GCD或更正确的方式做异步HTTP请求更好吗? – 2014-09-25 21:32:20