Arti
Arti

Reputation: 7762

Make sync request with NSURLSession

I call some function in another thread:

dispatch_async(dispatch_get_global_queue(priority, 0)) {
     self.someFunction()
}

Then in this function i want do synchronous request:

let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let dataSemaphore = dispatch_semaphore_create(0)

let task = session.dataTaskWithRequest(urlRequest, completionHandler: { (data, response, error) in
guard let responseData = data else {
    print("Error: did not receive data")
    return
}
guard error == nil else {
    print("error calling GET")
    print(error)
    return
    }
    print("ASYNC")
})
task.resume()

dispatch_semaphore_wait(dataSemaphore, DISPATCH_TIME_FOREVER)

print("SYNC")

But thread stops dispatch_semaphore_wait(dataSemaphore, DISPATCH_TIME_FOREVER) at this line

Upvotes: 1

Views: 982

Answers (2)

dgatwood
dgatwood

Reputation: 10407

Although the other answer here is technically correct, I feel the need to post a second answer, asking why you're doing this....

Synchronous networking is almost never the right way to do anything. Instead, you really should make the rest of the function be a block that gets run when the request finishes.

The fact that it is technically possible to create a synchronous wrapper for an async API doesn't negate the fact that doing so blocks whatever thread is running this code, so unless you're in a situation where you need to enforce some sort of strict ordering over network requests, synchronous requests are generally a bad idea, and even then, they aren't a good idea.

Upvotes: 0

Kametrixom
Kametrixom

Reputation: 14973

You never signaled the semaphore, put this at the end of your completion handler:

dispatch_semaphore_signal(dataSemaphore)

Upvotes: 1

Related Questions