Let session be a configured NSURLSession object. A typical code to create a task for this session in Swift would be,
let task = session.dataTaskWithRequest(request, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
//my custom completion handler invocation
})
My doubt is, does my custom completion handler be invoked in a dispatch_async call to avoid blocking the UI thread? Or it need not require, as the API is inherently asynchronous, the method dataTaskWithRequest(_:completionHandler) would take care of?
Also in the documentation, the method do not have a completion handler block as you can see. But I've seen many tutorials using this method!?
The completion handler runs on the queue that you provide, or on its own serial queue. Unless you explicitly pass in the main queue when you create the session, AFAIK, the only situation where callbacks would block the main thread would be if your completion handler explicitly calls performSelectorOnMainThread: or dispatch_async/sync with dispatch_get_main_thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With