Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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
Ios 如何检查dataTaskWithRequest是否已完成?_Ios_Swift_Closures_Nsurlsession - Fatal编程技术网

Ios 如何检查dataTaskWithRequest是否已完成?

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中,我想检查此任务是否

我是iOS编程新手,正在尝试创建我的第一个应用程序。我正在使用从服务器获取一些数据

  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()

啊!!这很有道理。谢谢