Drew C. Drew C. - 3 months ago 14
iOS Question

Are functions that take a closure automatically asynchronous?

I'm currently learning about callbacks in Swift and am trying to understand something very basic. I can't seem to find a straight answer on this anywhere.

I understand that if I do:

URLSession.dataTask(with: URLRequest) { data, response, error in }

it will perform an asynchronous network request, after which I can do my own work with the result in the completion handler.

However, let's say that I want to write my own function that does some sort of background task. Would simply having a closure as a parameter to the function (that gets called in the function body) be enough to make the function work asynchronously? Or would I have to get into calling GCD method(s) in the function body as well?


Simply having a callback parameter has no affect on the implementation of the method. If you wish to make it asynchronous then you need to take explicit steps to make it asynchronous. This can be done using grand central dispatch, operation queues, or calling other asynchronous methods.