Skip to content

Instantly share code, notes, and snippets.

@algal
Last active August 30, 2015 05:19
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 algal/6d62e53fb8d078ae7ae8 to your computer and use it in GitHub Desktop.
Save algal/6d62e53fb8d078ae7ae8 to your computer and use it in GitHub Desktop.
Pause execution in a playground until a condition is met (like an async network load completing)
// in a playground, pause execution wait until the condition pred is true
func waitUntilTrue(@autoclosure pred:()->Bool, secondsUntilTimeout duration:NSTimeInterval = 25)
{
let previousPlayGroundRunStatus = XCPExecutionShouldContinueIndefinitely()
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)
let start = NSDate()
while true {
if pred() {
NSLog("condition met.")
break
}
else if fabs(start.timeIntervalSinceNow) > duration {
NSLog("timeout")
break
}
else {
sleep(1)
}
}
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: previousPlayGroundRunStatus)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment