0

I need to repeate a piece of code in my method every second. I don't want to use NSTimer or something like that because the code needs to be in the same method because of some variables. I am looking maybe for a way with grand central dispatch but I have no idea how. Here is an example of what I want to achieve:

- (void)myMethod {

     // Repeat this code every second
     my awesome code
     // End of code repetition

     other awesome code
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
BalestraPatrick
  • 9,944
  • 4
  • 30
  • 43

1 Answers1

-2

int counter;

in .h file

counter=1;

in viewDidLoad

Whenever you want want not to repeat the code put counter=0;

 -(void)myMethod 
 {

 // Repeat this code every second
 if(counter==1)
 {  
    [self performSelector:@selector(myMethod) withObject:nil afterDelay:0.5];
 }

 // End of code repetition AUTOMATICALLY
}
Puru
  • 32
  • 7
  • It doesn't achieve the goal. It repeats the code *immediately*. Also it will crash if it repeats too many times. – Catfish_Man Sep 12 '13 at 01:39
  • @Catfish_Man: Ithink you hav't read this line : "Whenever you want want not to repeat the code put counter=0;" – Puru Sep 13 '13 at 06:01
  • The question wasn't about ending repetition. It was about repeating after a delay. – Catfish_Man Sep 13 '13 at 07:00
  • That's closer, but still doesn't meet the original goal. For reasons I can't understand, the person doesn't want to use selectors. I think that's silly, but it's what they asked. – Catfish_Man Sep 13 '13 at 18:47