Timer invalidate - objective-c

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.

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

NSEventTrackingRunLoopMode - this runs always?

I've added a timer with the runloopmode NSEventTrackingRunLoopMode like so:
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.50
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:YES];
[runLoop addTimer:timer forMode:NSEventTrackingRunLoopMode];
I thought this would only execute whilst for example a menu is open (modal), but it triggers the entire time, even when doing nothing. Is this the normal behavior?
Yes, this is normal behaviour. You add a timer to the runloopMode which is used for tracking events. This runloop runs all the time and is used to determine if there are events that needs to be processed and passed to your event handlers.

NSTimer not firing

I have an NSTimer that I init with this code:
testTimer = [[NSTimer alloc] initWithFireDate:[new objectAtIndex:0] interval:0.0 target:self selector:#selector(works:) userInfo:nil repeats:NO];
[new objectAtIndex:0] is an NSDate in the past.
When I start up the app, the timer is getting created, with a fireDate of immediately (since the date is in the past), however it never calls my works method. (-(void)works:(id)sender
)
Anybody know why this is happening?
You will have to add it to the current run loop if you use initWith.. method to create the timer object.
NSRunLoop * theRunLoop = [NSRunLoop currentRunLoop];
[theRunLoop addTimer:testTimer forMode:NSDefaultRunLoopMode];
Or if you would like it set up for you, use the scheduled... methods to create your timer.
I just recently had an issue with NSTimer. In my case I didn't realize that the method scheduledTimerWithTimeInterval is not multi thread safe. Once I moved the timer to the main thread it started working.
I think I had the same problem as Dobler, but my solution was different.
The problem was that the timer was being created and scheduled in a GCD thread in a block within a
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{})
call (actually nested deep, so it wasn't obvious that this was the case).
Using NSTimer's scheduledTimerWithTimeInterval:... placed the timer into an invalid run loop.
The fix was to change to
timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:#selector(...) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

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.