Reputation: 91
I'm creating a turn based game for the iPhone that contains animations between turns, and I want to wait on the [UIView animateWithDuration:...] method call inline in code. Is there a way to make this call synchronously instead of asynchronously? Currently what I am doing is...
// Some code...
NSConditionLock *conditionLock = [[NSConditionLock alloc] initWithCondition:0];
dispatch_sync(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:1
animations:^{
// some animation
}
completion:^(BOOL finished){
[conditionLock lock];
[conditionLock unlockWithCondition:1];
}];
});
// forces thread to wait until completion block is called
[conditionLock lockWhenCondition:1];
// More code...
Therefore in the above code, "// More code..." is only reached after the animation has completely finished. Obviously this code must run on a secondary thread, and it works as I want to. However, I have a feeling that using an NSConditionLock in combination with gcd is bad form and blocking the secondary thread in this way is not optimal for performance. Is my current code alright, or is there a better way to do this? Thanks.
Edit: The key point is that "// more code..." is inline, and not in the completion block. Really what I want to know, is it alright to use NSConditionLock's in combination with GCD, and if not what's the better way?
Upvotes: 1
Views: 839
Reputation: 5552
I would just put "//More code" in another method and call that method in the completion block. This will ensure that your code is only fired once your animation completes.
Upvotes: 2