为什么 NSURLSession.dataTaskWithURL() 不调用我的完成处理程序?
Why doesn't NSURLSession.dataTaskWithURL() call my completion handler?
我正在尝试从网络服务器加载 JSON
文件。以下是我如何开始请求:
let url:NSURL? = NSURL(string: lookupUrlFragment + query)
// Check if an actual url object was created
if let actualUrl = url {
// Create a default NSURLSessionConfiguration
let sessionConfig:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
// Create a default session
let session:NSURLSession = NSURLSession(configuration: sessionConfig)
session.dataTaskWithURL(actualUrl, completionHandler: {
(data:NSData?, response:NSURLResponse?, error:NSError?) in
NSLog("Got data = \(data)")
NSLog("Got response = \(response)")
NSLog("Got error = \(error)")
self.searchResults = data
self.delegate?.searchResultsAreReady()
})
}
我已经使用调试器单步调试了这段代码。当调用 dataTaskWithURL()
时,实际 Url 的值是正确的。如果我从网络浏览器中点击它,我会得到 JSON
文件。但是完成处理程序永远不会被调用。它永远不会在我在完成处理程序中设置的断点处停止,调试器日志中也没有输出。
我已经在单独的函数而不是闭包中使用完成处理程序进行了尝试,但行为是相同的。
谁能告诉我为什么我的完成处理程序没有被调用?
您忘记打电话给 resume()
。
let session:NSURLSession = NSURLSession(configuration: sessionConfig)
let task = session.dataTaskWithURL(actualUrl, completionHandler: {
(data:NSData?, response:NSURLResponse?, error:NSError?) in
NSLog("Got data = \(data)")
NSLog("Got response = \(response)")
NSLog("Got error = \(error)")
self.searchResults = data
self.delegate?.searchResultsAreReady()
})
task.resume() // you miss this
您从未开始任务。试试这个:
let url:NSURL? = NSURL(string: lookupUrlFragment + query)
// Check if an actual url object was created
if let actualUrl = url {
// Create a default NSURLSessionConfiguration
let sessionConfig:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
// Create a default session
let session:NSURLSession = NSURLSession(configuration: sessionConfig)
let task = session.dataTaskWithURL(actualUrl, completionHandler: {
(data:NSData?, response:NSURLResponse?, error:NSError?) in
NSLog("Got data = \(data)")
NSLog("Got response = \(response)")
NSLog("Got error = \(error)")
self.searchResults = data
self.delegate?.searchResultsAreReady()
})
task.resume()
}
我正在尝试从网络服务器加载 JSON
文件。以下是我如何开始请求:
let url:NSURL? = NSURL(string: lookupUrlFragment + query)
// Check if an actual url object was created
if let actualUrl = url {
// Create a default NSURLSessionConfiguration
let sessionConfig:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
// Create a default session
let session:NSURLSession = NSURLSession(configuration: sessionConfig)
session.dataTaskWithURL(actualUrl, completionHandler: {
(data:NSData?, response:NSURLResponse?, error:NSError?) in
NSLog("Got data = \(data)")
NSLog("Got response = \(response)")
NSLog("Got error = \(error)")
self.searchResults = data
self.delegate?.searchResultsAreReady()
})
}
我已经使用调试器单步调试了这段代码。当调用 dataTaskWithURL()
时,实际 Url 的值是正确的。如果我从网络浏览器中点击它,我会得到 JSON
文件。但是完成处理程序永远不会被调用。它永远不会在我在完成处理程序中设置的断点处停止,调试器日志中也没有输出。
我已经在单独的函数而不是闭包中使用完成处理程序进行了尝试,但行为是相同的。
谁能告诉我为什么我的完成处理程序没有被调用?
您忘记打电话给 resume()
。
let session:NSURLSession = NSURLSession(configuration: sessionConfig)
let task = session.dataTaskWithURL(actualUrl, completionHandler: {
(data:NSData?, response:NSURLResponse?, error:NSError?) in
NSLog("Got data = \(data)")
NSLog("Got response = \(response)")
NSLog("Got error = \(error)")
self.searchResults = data
self.delegate?.searchResultsAreReady()
})
task.resume() // you miss this
您从未开始任务。试试这个:
let url:NSURL? = NSURL(string: lookupUrlFragment + query)
// Check if an actual url object was created
if let actualUrl = url {
// Create a default NSURLSessionConfiguration
let sessionConfig:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
// Create a default session
let session:NSURLSession = NSURLSession(configuration: sessionConfig)
let task = session.dataTaskWithURL(actualUrl, completionHandler: {
(data:NSData?, response:NSURLResponse?, error:NSError?) in
NSLog("Got data = \(data)")
NSLog("Got response = \(response)")
NSLog("Got error = \(error)")
self.searchResults = data
self.delegate?.searchResultsAreReady()
})
task.resume()
}