Wait for a timer or a condition to become true and then process the code? (Wait for a iOS camera to adjust focus)
grand-central-dispatch, ios, objective-c
Solution
Since you are not in control of `AVCaptureStillImageOutput`'s `isAdjustingFocus` (you are not the one setting it to true or false) then you can't use my previous answer (that's what I meant by I would need the exact situation: what are we waiting for, and why. Implementation details depend on these informations).
IMHO, the best option would indeed be to implement some timeout and wait for it just as you suggested. Be sure to use `usleep()` so you aren't polling continuously.
NSDate* date = [NSDate date];
while (TRUE)
{
if (myBOOL)
{
// the condition is reached
break;
}
if ([date timeIntervalSinceNow] < -5)
{
// the condition is not reached before timeout
break;
}
// adapt this value in microseconds.
usleep(10000);
}
Problem
I want to process some code after some condition checking. First of it is that some variable must be `true` (I have a Key-Value Observer assigned to it). Second - if that variable hasn't become `true` for some time (e.g. 5 seconds), then nevermind the variable and just process the code. I come with an obvious solution, which, I think, is bad: infinite `while()` loop in another dispatch queue, every time checking the `true` condition and time passed. And all this code is wrapped in another dispatch queue... well, does not look good for me. The draft pseudocode of what I want to do: ``` WHEN (5 seconds are gone || getCurrentDynamicExpr() == true) { processStuff(); } ``` What's the right and easy way to do this? EDIT Seems a lot of confusion here... Got to be more concrete: I want to capture a camera shot when it's focused, so I want to check `AVCaptureDevice`'s `isAdjustingFocus` property (I'm using `AVCaptureStillImageOutput`), and then capture a shot. 5 seconds are for.. well, if it didn't focus then something is wrong, so take the picture anyway. I'm sorry about a confusion, thought it's something really common..