Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean "NSURLSession API is highly asynchronous"?

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!?

like image 927
Viki Avatar asked Jul 03 '26 00:07

Viki


1 Answers

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.

like image 133
dgatwood Avatar answered Jul 04 '26 14:07

dgatwood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!