views:

146

answers:

2

I have a method that I need to repeat until a certain condition is met. I am using an statement like:

if (condition is not met){

run this method again

} else {

}

But I don't know how to 'run this method again'. The method is called runAction so i tried [self runAction] but it caused a runtime error.

Any help appreciated.

Thanks

+3  A: 

Calling a method from within itself is legal, but you may end up with a stack overflow if you call unto infinity

- (void)runAction
{
    [self runAction]; // Stack Overflow on this line ;)
}
rpetrich
+2  A: 

rpetrich has given you the right answer, but have you considered using a loop instead?

while (condition is not met)
{
  // logic that affects condition.
}

Also if 'condition' is dependent on outside forces (user input, downloading, etc) then neither of these are correct and you could cause a deadlock - this is where two cases cannot complete because they are both waiting on the other.

If this is the case you should use a timer that checks for the condition every XXX seconds or so. The easiest way is to use the 'performSelector' function which can be used to trigger a call after a specified amount of time.

E.g.

-(void)someFunc
{
  if(!condition)
  {
    [self performSelector:@selector(someFunc) withObject:nil afterDelay:1.0];
  }
}
Andrew Grant
You might also want to look into a NSCondition rather than using a busy-wait loop.
Jesse Rusak