How to release NSTimer in ASCellNode - objective-c

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.

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

Pause and resume Timer in ViewController

I want to pause and resume timer in viewcontroller which is displayed as subview in mainviewcontroller. Play/Pause button in mainviewcontroller when pressed it displays view controller as subview as well as plays audio.
Have this NSTimer in view controller
[NSTimer scheduledTimerWithTimeInterval:2.6
target:self
selector:#selector(updateView:)
userInfo:nil
repeats:YES];
- (void)updateView:(NSTimer *)theTimer
{
if (index < [textArray count])
{
self.textView.text = [self.textArray objectAtIndex:index];
self.imageView.image = [self.imagesArray objectAtIndex:index];
index++;
} else {
index = 0;
}
I want to pause and resume timer when play/pause button is pressed in mainviewcontroller. How can i do that.
NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval:2.6
target:self
selector:#selector(updateView:)
userInfo:nil
repeats:YES];
[myTimer invalidate];
/*
Stops the receiver from ever firing again and requests its removal from its run loop.
*/
[myTimer fire];
/*
You can use this method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it is automatically invalidated after firing, even if its scheduled fire date has not arrived.
*/
First you have to keep a reference of your timer in your interface-file:
NSTimer *myTimer;
In the implementation file you can simply connect and then refer to it:
myTimer = [NSTimer scheduledTimerWithTimeInterval:2.6
target:self
selector:#selector(updateView:)
userInfo:nil
repeats:YES];
To stop the timer call:
[myTimer invalidate];
To start it again:
[myTimer fire];
See NSTimer Class Reference for more info about the NSTimer Class.

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

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.