0

Another quick one:

If I perform something like this:

runningAnimation = YES;
[self performSelector:@selector(animationsComplete) withObject:nil afterDelay:0.1[;

// Return to main function
-(void) mainFunction
{
while (runningAnimation)
{
continue;
}
}

// And, animationsComplete looks like this:
-(void) animationsComplete
{
runningAnimation = NO;
}

the Program never seems to get out of the while loop. Can anyone tell me why this is?

On another note, if this type of "wait in my code for something to finish executing" can't really ever work in the sense that I was trying to do, is there another way to do the same thing? To just pause in a function whilst waiting for a call of

[self performSelector:withObject:afterDelay:]

to complete? Sorry if this seems like an amateur question. I'm an amateur.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
J-Rock
  • 95
  • 1
  • 6

2 Answers2

0

That's because your "performSelector:" is happening on your current (main) thread.

If you add a line like "NSLog( @"in main function");" inside that "while" loop, as soon as your performSelector fires up, it should stop.

You need to do the performSelector on another (detached) thread, or use a block. Some blocks can use "stop" Booleans in their arguments.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • So what -EXACTLY- happens when I call performSelector:withObject:afterDelay: Preexisting conditions are that the performSelector: method that is called and the method that calls the performSelector: are both in the same main thread. Does the program execution get to the performSelector:afterDelay: command and then continue executing code? Or does it stop until the afterDelay: time period has elapsed? I feel like if I understood -exactly- what was going on at a very "close to the metal" level, I would be able to use the command better... – J-Rock Feb 04 '13 at 01:28
0
WHILE (ANIMATIONCOMPLETE != 1) {
... Do work ...
... When work is done, like using if code... ...
IF (SomeBoolean // That means work done == (0 for false, continue, 1 for true, break)) {
BREAK;
}
ELSE {
CONTINUE
}
}
Whippet
  • 322
  • 1
  • 11
  • So if I call a performSelector from -inside- a while loop that then calls a method that makes the while loop condition stop executing, that would work? – J-Rock Feb 04 '13 at 01:38