Xcode - NSTimer firing at random intervals? No consistency - objective-c

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];
}

Related

Start & Stop Timer

I have a timer that calls a function every 10 seconds:
[NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:#selector(checkForMessages)
userInfo:nil
repeats:YES];
It calls the function:
- (void)checkForMessages
{
//do something here
}
Two questions:
How do I stop the timer if needed?
Can I put this timer and function somewhere so that I can call it from different view controllers?
"scheduledTimerWithTimeInterval" returns an "NSTimer" object.
If you hold onto that object (e.g. set and get it via a "property"), you can stop it via the NSTimer invalidate method.
And since you're asking for code, add this to your view controller's .h #interface:
#property (strong) NSTimer * messageTimer;
Then, in your view controller's .m file:
self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:#selector(checkForMessages)
userInfo:nil
repeats:YES];
Makes sense?

NSTimer stop when app is hide?

I just want to know why a NSTimer is stoping when the application is hidden or "Hide other" in an other application ?
how can I remedy this ?
Thanks
Maybe your timer is getting garbage collected or otherwise freed perhaps by the autorelease pool, and hence does not fire anymore.
Try doing this:
- (IBAction)recordCam1:(id)sender {
myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(recordFile1:) userInfo:Nil repeats:YES];
[myTimer retain];
}
where myTimer is an instance variable in your class. See if that produces a different behaviour. If so, then you know the problem is to do with memory management.

NSTimer get executed but does not calls the function

Im having a problem with a NSTimer and i really dont know why this is not working!
i got this
.h
NSTimer eventtimer;
.m
eventtimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(CheckForAlarm) userInfo:nil repeats:YES];
The problem is that this timer gets executed but never calls that function..
What am i doing wrong??
in .m direct use this without declare in NSTimer
[NSTimer scheduledTimerWithTimeInterval:62 target:self selector:#selector(CheckForAlarm) userInfo:nil repeats:YES];//where required
-(void)CheckForAlarm
{
//your logic
}

NSTimer crash with EXC_BAD_ACCESS in shared class

I have an NSTimer running in a shared class. + (GlobalClass *)sharedInstance;
Basically it runs once, and the second time it runs, it just killed the whole app.
This is how I'm doing the NSTimer
myTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:#selector(moveMe)
userInfo:nil
repeats:YES];
method moveMe is just an empty method for now. So it shouldn't be something that's happening within moveMe.
Has anyone experienced this?
It looks like you're missing the colon in your selector name. The selector for NSTimer takes an NSTimer as an argument. Your code that creates the timer should look like this:
myTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:#selector(moveMe:)
userInfo:nil
repeats:NO];
Note the colon after moveMe. Your method should then look something like this:
- (void)moveMe:(NSTimer *)aTimer {
// Code
}

Timer invalidate

What is the meaning of this statement given?
NSTimer *timer ,[timer invalidate]
It's an objective C timer statement that cancels a running timer.
Normally it would be expressed as:
NSTimer* myTimer = [NSTimer timerWithTimeInterval:1.0 target:self
selector:#selector(calculateTLE) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:myTimer forMode: NSDefaultRunLoopMode];
..
[myTimer invalidate];
It looks as though you're creating an instance of NSTimer without defining it, and then in the same line for whatever reason stopping that same timer. Waste of memory allocation resources, unless you're planning to use the timer at a later time, in which case you should trigger the timer then.