itai alter
itai alter

Reputation: 601

NSTimer freezes the app until it gets fired again?

I have a simple app with a button, UIImageView and a NSTimer. The timer is fired up every 5 seconds repeatedly to update the ImageView with a new image, while the button simply stops the timer and switches to another View.

The problem is that when I press the button, nothing happens for a few seconds (until the timer fires up again).

Is there a way to cause the button to stop the timer and do its job at any given time instead of between intervals of the timer?

The button is defined in the InterfaceBuilder and connected to a function in the code.

The NSTimer code: (I've tried with & without adding it to the RunLoop).

updateTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(updateImageViews) userInfo:nil repeats:YES];

NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
[[NSRunLoop currentRunLoop] addTimer:updateTimer forMode:NSDefaultRunLoopMode];
[runLoop run];

Thanks!

Upvotes: 1

Views: 646

Answers (1)

cheese
cheese

Reputation: 45

"Note that from the perspective of NSRunloop, NSTimer objects are not "input"—they are a special type, and one of the things that means is that they do not cause the run loop to return when they fire." (from NSRunLoop Class Reference) - so I think you block the runloop from continuing? Perhaps put the timer to a different thread as solution? I have problems on this on my own, too :(

Upvotes: 1

Related Questions