NSTimer get executed but does not calls the function - objective-c

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
}

Related

How to release NSTimer in ASCellNode

I create a NSTimer in ASCellNode, when ASTableView's UIViewController pop, but the timer not be released.
_timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(autoScrollBanner) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
how can i stop this timer?
thx in advance!
Try overwrite next methods didExitVisibleState, didExitDisplayState,didExitHierarchy in ASCellNode inherited object and insert cancel fired timer.

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

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 doesn't call method

I'm really frustrated now, googled the whole internet, stumbled through SO and still didn't find a solution.
I'm trying to implement an NSTimer, but the method which I defined doesn't get called. (seconds are set correctly, checked it with breakpoints). Here is the code:
- (void) setTimerForAlarm:(Alarm *)alarm {
NSTimeInterval seconds = [[alarm alarmDate] timeIntervalSinceNow];
theTimer = [NSTimer timerWithTimeInterval:seconds
target:self
selector:#selector(showAlarm:)
userInfo:alarm repeats:NO];
}
- (void) showAlarm:(Alarm *)alarm {
NSLog(#"Alarm: %#", [alarm alarmText]);
}
The object "theTimer" is deined with #property:
#interface FooAppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate> {
#private
NSTimer *theTimer;
}
#property (nonatomic, retain) NSTimer *theTimer;
- (void) setTimerForAlarm:(Alarm *)alarm;
- (void) showAlarm:(Alarm *)alarm;
What am I doing wrong?
timerWithTimeInterval simply creates a timer, but doesn't add it to any run loops for execution. Try
self.theTimer = [NSTimer scheduledTimerWithTimeInterval:seconds
target:self
selector:#selector(showAlarm:)
userInfo:alarm repeats:NO];
instead.
Also don't forget to check if
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
target:(id)target
selector:(SEL)aSelector
userInfo:(id)userInfo
repeats:(BOOL)repeats
is called in the main thread.
You've created an NSTimer object but you haven't scheduled it to be run. timerWithTimeInterval:target:selector:userInfo:repeats: creates a timer that you can schedule to run later, for example, to create a timer at application launch and have it start counting when the user presses a button. Either call
[[NSRunLoop currentRunLoop] addTimer:theTimer forMode:NSDefaultRunLoopMode]
at the end of setTimerForAlarm or replace
theTimer = [NSTimer timerWithTimeInterval:seconds
target:self
selector:#selector(showAlarm:)
userInfo:alarm repeats:NO];
with
theTimer = [NSTimer scheduledTimerWithTimeInterval:seconds
target:self
selector:#selector(showAlarm:)
userInfo:alarm repeats:NO];
which creates a timer and immediately schedules it.
Well you may want to actually schedule your NSTimer on the run loop :) instead of timerWithTimeInterval use scheduledTimerWithTimeInterval.
theTimer = [NSTimer scheduledTimerWithTimeInterval:seconds
target:self
selector:#selector(showAlarm:)
userInfo:alarm repeats:NO];
While all of the answers are right, there is an even simpler solution that doesn't involve a NSTimer at all. Your setTimerForAlarm: implementation can be reduced to one simple line:
[self performSelector:#selector(showAlarm:) withObject:alarm afterDelay:[[alarm alarmDate] timeIntervalSinceNow]]

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
}