I am creating a simple game.
I have the following code:
- (void)doStuff
{
double delayInSeconds = [NSNumber randomFloatBetweenLowerBound:0.8f upperBound:2.6f];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if ([self shouldDoMoreStuff]) {
[self doStuff];
}
});
}
This method starts in viewDidLoad, but it also starts when the player dies (after clicking "Try Again").
After several deaths, in the end, many iterations of this method are performed simultaneously, and not just one.
I don’t know much about GCD, NSOperationetc., but I’m sure I should use something to control this - perhaps NSOperation- so viewDidLoadit starts up in it, and then when the player dies, I cancel the operation and restart it .
, NSOperation, , :
a)
) .
- , ?
, .