0

I am making an app that has a king of "update method" that is responsible for running code continually. I thought of using an NSTimer as it has an option to have it repeat indefinitely. I have it load as such:

gameTimer = [NSTimer timerWithTimeInterval:0.01428 target:self selector:@selector(GameUpdate:) userInfo:NULL repeats:true];

Where the GameUpdate: declaration is like so:

-(void)GameUpdate:(NSTimer*)timer;

The thing is, the code inside GameUpdate: never runs. It used to work in xcode 4.2. Why is this?

rdelfin
  • 819
  • 2
  • 13
  • 31

1 Answers1

0

You need to add your newly created NSTimer object to the run loop.

(lifted from the answer to this related question): As the docs say:

Timers work in conjunction with run loops. To use a timer effectively, you should be aware of how run loops operate—see NSRunLoop and Threading Programming Guide. Note in particular that run loops retain their timers, so you can release a timer after you have added it to a run loop.

If you look at the other answers to that related question, you'll see how to create a NSTimer object where it gets automatically added to the run loop.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • 1
    Thanks. While there was no need to directly use the NSRunLoop, I saw an answer in the link for the question you provided. It had to be a scheduled Timer instead of just a timer. – rdelfin May 29 '12 at 02:08