NSURLSession completion block not called

esh picture esh · Jun 5, 2014 · Viewed 13.9k times · Source
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.

What's wrong?

Also I tried a synchronous and asynchronous form of the same request with NSURLConnection and it worked perfectly.

EDIT:

I tried assigning a dataTask variable to the session.dataTaskWithRequest and displayed it right after. It says this <__NSCFLocalDataTask: 0xb41bda0> { suspended } Suspended? Why?

Answer

esh picture esh · Jun 5, 2014

So I tried calling it like this

session.dataTaskWithRequest(urlRequest, 
                        completionHandler: {(data: NSData!, 
                                             response: NSURLResponse!,                       
                                             error: NSError!) in
                                                  print(data)
                                                  print(response)
                                                  print(error)
                                           }).resume()

And it worked.

Seems like I have to call resume() on a default suspended session task.