Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift-网络请求和后台队列_Swift_Api_Networking_Grand Central Dispatch - Fatal编程技术网

Swift-网络请求和后台队列

Swift-网络请求和后台队列,swift,api,networking,grand-central-dispatch,Swift,Api,Networking,Grand Central Dispatch,只是想澄清一下在Swift 2中进行网络api调用的最佳实践 以下是我下载JSON数据的典型网络请求: let session = NSURLSession(configuration: .defaultSessionConfiguration()) let url = NSURL(string: my_url_string) let request = NSURLRequest(URL: url) let dataTask = session.dataTaskWithRequest(requ

只是想澄清一下在Swift 2中进行网络api调用的最佳实践

以下是我下载JSON数据的典型网络请求:

let session = NSURLSession(configuration: .defaultSessionConfiguration())
let url = NSURL(string: my_url_string)
let request = NSURLRequest(URL: url)

let dataTask = session.dataTaskWithRequest(request) { data, response, error in
   do {
       self.tableData = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! [NSDictionary]
       dispatch_async(dispatch_get_main_queue(), { () -> Void in
          self.tableView.reloadData()
       })
   } catch let error {
        print(error)
   }
}
dataTask.resume()
我的问题是:我是否应该将所有代码块包装到后台队列中?我应该这样做:

let download_queue = dispatch_queue_create("download", nil)
dispatch_async(download_queue) { () -> Void in
     previous code here
}
或者我应该使用一个给定的高优先级队列,例如:

dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0)

另外,在后续视图控制器上进行其他网络请求时,我应该使用此处使用的相同队列还是创建一个新队列?

默认情况下
NSURLSession
API是高度异步的。来自苹果的有用信息


并没有明显的问题表明要用
GCD
包装您的代码块,并且完成块在后台线程上运行,所以正确使用
GCD
更新
UITableview

非常感谢!你说我不需要改变我的原始代码?就我所见,没有必要使用额外的GCD块。但这一切都没关系。