Creating an iOS Timer - objective-c

I am trying to create a "stop watch" type functionality. I have one label (to display the elapsed time) and two buttons (start and stop the timer). The start and stop buttons call the startTimer and stopTimer functions respectively. Every second the timer fires and calls the increaseTimerCount function. I also have an ivar timerCount which holds on to the elapsed time in seconds.
- (void)increaseTimerCount
{
timerCountLabel.text = [NSString stringWithFormat:#"%d", timerCount++];
}
- (IBAction)startTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(increaseTimerCount) userInfo:nil repeats:YES];
}
- (IBAction)stopTimer
{
[timer invalidate];
[timer release];
}
The problem is that there seems to be a delay when the start button is pressed (which I am assuming is due to reinitializing the timer each time startTimer is called). Is there any way to just pause and resume the timer without invalidating it and recreating it? or a better/alternate way of doing this?
Thanks.

A bit dated but if someone is still interested...
don't "stop" the timer, but stop incrementing during pause, e.g.
- (void)increaseTimerCount
{
if (!self.paused){
timerCount++
}
timerCountLabel.text = [NSString stringWithFormat:#"%d", timerCount];
}

You can't pause the timer without using invalidate. What you can do is add
[timer fire];
after you create the timer in startTimer.

Related

Xcode - NSTimer firing at random intervals? No consistency

So I am using NSTimer to run a function every minute, it fires at the correct time for the first 3 or so attempts and then it suddenly starts firing every second. I have no idea why this is happening? Would anyone be able to let me know as to why NSTimer is firing inconsistently?
Here is the line where I have declared my timer.
[NSTimer scheduledTimerWithTimeInterval:60.0f target:self selector:#selector(checkForLocation) userInfo:nil repeats:YES];
It is worth noting that I have NSTimer declared inside of viewDidAppear.
Any help is appreciated,
Thank you.
Try to create a instance for NSTimer by declaring a property. Write a function which will initialize the timer and don’t forget to invalidate it before re-intializing it. Call initializeMyTimer in your -viewDidAppear.
-(void) initializeMyTimer
{
if(myTimer)
{
[myTimer invalidate];
myTimer = nil;
}
myTimer = [NSTimer scheduledTimerWithTimeInterval:60.0f
target:self
selector:#selector(checkForLocation)
userInfo:nil
repeats:YES];
}

NSTimer Will Not Invalidate

-(IBAction) loadWeb: (id) sender {
[_webView loadRequest:nsrequest2];
_webView1.hidden = YES;
_webView.hidden = NO;
self.checkForAdd = [NSTimer scheduledTimerWithTimeInterval:0.4
target:self selector:#selector(checkForAddToCart:) userInfo:nil
repeats:YES];
}
-(IBAction)button1:(id)sender {
[self.checkForAdd invalidate];
}
How would I invalidate the timer? I have tried it without self and many other ways, but for some reason, when I press the button the timer does not invalidate.
Check if loadWeb: is called multiple times. If it is, you will have old timers running without having a reference to them so you can't invalidate them. You should have [self.checkForAdd invalidate]; before you create the new timer.
When you do invalidate the timer, if you aren't creating a new one, set self.checkForAdd = nil; to be sure you aren't going to try using the timer again (some actions will throw an exception if you do).
If at any point you do self.checkForAdd = nil; without having invalidated the timer then you won't have a reference to it so you won't be able to invalidate it in the future.
According to your code, loadWeb is being trigger by a button. So you will be creating new timer every time when button will be press. Its better you create timer on some where else, like create in init, or in viewDidLoad method, because if you are creating this here, you have to make sure you are not creating timer again and again. You can do this by doing a if check
if(!self.checkForAdd){
self.checkForAdd = [NSTimer scheduledTimerWithTimeInterval:0.4
target:self selector:#selector(checkForAddToCart:) userInfo:nil
repeats:YES];
}
Right now its creating new timer on button tap, and previous one have no reference.

iOS5 ARC is it safe to schedule NSTimers from background selectors?

I'm trying to debug my application.
I've been using some NSTimer instances in my non-arc code like this (from the main thread):
[NSTimer scheduledTimerWithTimeInterval:5 target:musicPlayer selector:#selector(playPause:) userInfo:nil repeats:NO];
This works fine if I assign this code to a button and click a button. The timer fires.
I've also tried:
if( self.deliveryTimer == nil)
{
self.deliveryTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(playPause:) userInfo:nil repeats:NO];
}
-(void)playPause:(NSTimer*)timer
{
[deliveryTimer invalidate];
deliveryTimer = nil;
//more code here
}
I would expect the timer to execute, hit the play/pause method below, then turn to nil, so I can reset the timer later. The reason why I'm checking for nil is because I have 3 different code paths that may set the timer. Each one has an NSLog statement indicating that the timer has been scheduled.
My code runs, and I see that the timers are being scheduled, but they don't seem to fire in the course of normal app execution. I'm investigating why. Short term timers, using the same logic fire fine. It is when I let the app run for a while that I'm running into issues.
Could the NSTimers be reclaimed by ARC?
Does it matter if I set the timer from a performSelectorInBackground? As I was writing up this question, I noticed that some of my timers were created from a code path that is being called through:
[self performSelectorInBackground:#selector(notifyDelegateOfDataPoint:) withObject:data];
could the background selector be the reason why my timers do not fire/get reclaimed earlier?
Any help is appreciated, this bug has been bugging me for over 2 weeks!
Update: after changing the code to use the main thread for NSTimers, the timers fire correctly, causing the music to play:
[self performSelectorOnMainThread:#selector(deliverReminder:) withObject:nil waitUntilDone:NO];
-(void)deliverReminder:(id)sender{
[ NSTimer scheduledTimerWithTimeInterval:10 target:reminderDeliverySystem selector:#selector(playAfterDelay:) userInfo:nil repeats:NO];
[self postMessageWithTitle:nil message:#"Deliver Reminder Called" action:kNoContextAction];
}
-(void)playAfterDelay:(id)sender
{
int reminderDelay = reminder.delayValue.intValue;
[playTimers addObject:[NSTimer scheduledTimerWithTimeInterval:reminderDelay target:self selector:#selector(appMusicPlayerPlay:) userInfo:nil repeats:NO]];
}
Here I have a whole bunch of timers, which is because I don't know how to pass a primitive to a target with a selector.
An NSTimer requires a run loop to be running in that background thread for it to keep firing. The main thread already has an active run loop, which is why your timers work fine when executed on it.
If you want to use your timers within a background thread, you can do something like the following:
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
self.deliveryTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(playPause:) userInfo:nil repeats:NO];
[runLoop run];
What was probably happening with your short duration timers firing, but the longer ones not, was that they were firing while the thread was still active, but without a run loop to keep it going, were failing after the thread reached the end of its execution.
I don't believe this is ARC-related, although there may be something there you'll have to watch for, because the NSRunLoop holds on to a timer that is attached to it. Following standard procedure with NSTimers should avoid ARC problems.

How can I reset a timer?

I have an app, that every 3 seconds, fires an event that goes to my server and grabs information. I would like to reset the timer count down if an event has transpired in between the time the timer last fired and when it fires subsequently.
So essentially, if my other event fires at 2.5 seconds, and the timer is set to fire in .5 seconds, I would like to reset the timer back to 3 seconds. How can I accomplish this?
I declare the timer as:
_timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target: self
selector: #selector(getUpdates)
userInfo:nil
repeats: YES];
And psuedo:
-(void) anEventHappened
{
// I got something from the server, I don't need to query it. Reset timer here.
}
-(void) getUpdates
{
// I received no reset, I should check for an update.
}
Does this work?
[_timer invalidate];
_timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target: self
selector: #selector(getUpdates)
userInfo:nil
repeats: YES];

Making a Method happen every 60 seconds when the App is running

I'm wanting to make one of my methods to run every 60 seconds when my App is running, how would I do that?
NSTimer
- (void) startTimer
{
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:60
target:self
selector:#selector(timerFired:)
userInfo:nil
repeats:YES];
}
- (void) stopTimer
{
[self.myTimer invalidate];
}
- (void) timerFired:(NSTimer*)theTimer
{
NSLog(#"yay");
}
While the answer is valid, your question is incomplete.
Why do you need to run this method regularly? If it is to poll iCal for tasks every 60 seconds this isn't the best solution. What you need to be doing is observing the notifications that CalCalender store puts outs