Skip to content

why does the Swift code generated by Postman use semaphores? #10053

@mattneub

Description

@mattneub

Start with a simple GET request in Postman, and then ask Postman to generate the code for the corresponding Swift equivalent using URLSession. You'll get something with this form:

var semaphore = DispatchSemaphore (value: 0)
var request = //
let task = URLSession.shared.dataTask(with: request) { data, response, error in 
  guard let data = data else {
    print(String(describing: error))
    semaphore.signal()
    return
  }
  print(String(data: data, encoding: .utf8)!)
  semaphore.signal()
}
task.resume()
semaphore.wait()

This is wrong. One should basically never use semaphores, and certainly there is no need for them here. The correct answer to the question that we're asking Postman, how to perform this same request in Swift, is to take all of that code and delete every line containing the word "semaphore". So, since everyone is always going to have to do that, why include those lines to begin with?

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions