Last active
January 19, 2020 04:34
-
-
Save zentrope/707fe387eaf7d07c808d9e76d449640f to your computer and use it in GitHub Desktop.
Use a semaphore to block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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