为什么NSURLSession.dataTaskWithURL()不调用我的完成处理程序?

时间:2021-12-31 21:00:54

I'm trying to load a JSON file from a web server. Here's how I kick off the request:

我正在尝试从Web服务器加载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()
   })
}

I've stepped through this code with the debugger. When it gets to the invocation of dataTaskWithURL() the value of actual Url is correct. If I hit it from a web browser, I get the JSON file. But the completion handler never gets called. It never stops at a break point I set in the completion handler, and no output appears in the debugger log.

我已经使用调试器完成了这段代码。当它调用dataTaskWithURL()时,实际Url的值是正确的。如果我从网络浏览器点击它,我会得到JSON文件。但完成处理程序永远不会被调用。它永远不会在我在完成处理程序中设置的断点处停止,并且调试器日志中不会显示任何输出。

I've tried this with the completion handler in a separate function instead of a closure, but the behavior is the same.

我已经尝试使用完成处理程序在一个单独的函数而不是一个闭包,但行为是相同的。

Can anyone tell me why my completion handler isn't getting called?

谁能告诉我为什么我的完成处理程序没有被调用?

2 个解决方案

#1


5  

You forgot to call 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

#2


0  

You are never starting the task. Try 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()
}

#1


5  

You forgot to call 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

#2


0  

You are never starting the task. Try 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()
}