Skip to content

Instantly share code, notes, and snippets.

@zentrope
Last active January 19, 2020 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zentrope/707fe387eaf7d07c808d9e76d449640f to your computer and use it in GitHub Desktop.
Save zentrope/707fe387eaf7d07c808d9e76d449640f to your computer and use it in GitHub Desktop.
Use a semaphore to block
func xmlDocument(url: String) -> Document? {
var doc: Document?
let semaphore = DispatchSemaphore(value: 0)
AF.request(url).response { response in
if let data = response.data, let xml = String(data: data, encoding: .utf8) {
do {
doc = try SwiftSoup.parse(xml, "", Parser.xmlParser())
} catch {
print("error: \(error.localizedDescription)")
}
semaphore.signal()
}
}
// This blocks until the other thread calls .signal()
semaphore.wait()
return doc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment