Ios 如何检查dataTaskWithRequest是否已完成?
我是iOS编程新手,正在尝试创建我的第一个应用程序。我正在使用从服务器获取一些数据Ios 如何检查dataTaskWithRequest是否已完成?,ios,swift,closures,nsurlsession,Ios,Swift,Closures,Nsurlsession,我是iOS编程新手,正在尝试创建我的第一个应用程序。我正在使用从服务器获取一些数据 var task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in 我知道completionHandler闭包中的所有代码都是在任务完成时执行的。在我的ViewController中,我想检查此任务是否
var task = NSURLSession.sharedSession().dataTaskWithRequest(request,
completionHandler: { (data, response, error) -> Void in
我知道completionHandler闭包中的所有代码都是在任务完成时执行的。在我的ViewController中,我想检查此任务是否已完成,并在完成之前不加载表。如何检查此任务是否已完成
我想我可以让completionHandler在运行时将某个全局布尔变量设置为true,我可以在我的ViewController中检查该变量,但我觉得有更好的方法使用内置功能,我只是不知道 视图控制器不需要知道何时调用了
completionHandler
。您所要做的就是让completionHandler
实际将tableView.reload()
分派回主队列(然后触发调用UITableViewDataSource
方法)。启动UI更新的是completionHandler
,而不是相反:
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
// check for errors and parse the `data` here
// when done
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reload() // this results in all of the `UITableViewDataSource` methods to be called
}
}
task.resume()
啊!!这很有道理。谢谢