Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLSession completion block not called

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?

like image 631
esh Avatar asked Jun 05 '14 06:06

esh


2 Answers

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.

like image 72
esh Avatar answered Oct 19 '22 23:10

esh


Are you using playgrounds??

If you are, you should be careful to include:

 XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)

In order to make the playground wait for the callback

like image 43
supersabbath Avatar answered Oct 20 '22 00:10

supersabbath