Last active
August 30, 2015 05:19
-
-
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)
This file contains hidden or 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
// 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