未调用NSURLSession完成块 [英] NSURLSession completion block not called
本文介绍了未调用NSURLSession完成块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var session = NSURLSession.sharedSession()
session.dataTaskWithRequest(urlRequest,
completionHandler: {(data: NSData!,
response: NSURLResponse!,
error: NSError!) in
println(data)
println(response)
println(error)
})
所以我发出了这个请求,永远不会调用完成块。
So I am making this request, and the completion block is never called.
出了什么问题?
我还尝试了一个同步和异步表单
与 NSURLConnection
相同的请求,并且效果很好。
Also I tried a synchronous and asynchronous form
of the same request with NSURLConnection
and it worked perfectly.
编辑:
我尝试将 dataTask
变量分配给 session.dataTaskWithRequest
并显示它之后。它说这个< __ NSCFLocalDataTask:0xb41bda0> {suspended}
暂停?为什么?
I tried assigning a dataTask
variable to the session.dataTaskWithRequest
and displayed it right after. It says this <__NSCFLocalDataTask: 0xb41bda0> { suspended }
Suspended? Why?
推荐答案
所以我试着这样称呼它
session.dataTaskWithRequest(urlRequest,
completionHandler: {(data: NSData!,
response: NSURLResponse!,
error: NSError!) in
print(data)
print(response)
print(error)
}).resume()
它有效。
似乎我必须在默认的暂停会话任务上调用 resume()
。
Seems like I have to call resume()
on a default suspended session task.
这篇关于未调用NSURLSession完成块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文