session.dataTaskWithURL completionHandler 从未被调用 [英] session.dataTaskWithURL completionHandler never called

查看:70
本文介绍了session.dataTaskWithURL completionHandler 从未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

let urlPath:String = apiURL + apiVersion + url + "?api_key=" + apiKey
let url = NSURL(string: urlPath)
let session = NSURLSession.sharedSession()
println(url!)
let task = session.dataTaskWithURL(url!, completionHandler: {(data, reponse, error) in
    println("Task completed")
    // rest of the function...
})

completionHandler 函数永远不会被调用.我尝试在浏览器中调用 URL,它工作正常.我尝试了另一个 URL,它仍然不起作用.我检查了我的 ios 模拟器是否可以连接到互联网,确实如此.

The completionHandler function is never called. I tried calling the URL in my browser, it works fine. I tried with another URL, it still doesn't work. I checked that my ios simulator could connect to the Internet, it does.

我不知道为什么没有调用该函数,而且由于我没有任何错误,因此很难调试.

I don't know why the function is not called and since I don't have any error it's hard to debug.

推荐答案

任务永远不会完成,因为它永远不会开始.您必须使用其 resume() 方法手动启动数据任务.

The task never completes because it never gets started. You have to manually start the data task using its resume() method.

let urlPath = apiURL + apiVersion + url + "?api_key=" + apiKey
let url = NSURL(string: urlPath)!
let session = NSURLSession.sharedSession()

let task = session.dataTaskWithURL(url) { data, response, error in
    print("Task completed")
    // rest of the function...
}

task.resume()

这篇关于session.dataTaskWithURL completionHandler 从未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆