ns timer duplicate created 2-3 times - ios

I am working on a app in which i have created a timer which called method make sound every 1 sec..
after 10 sec it is invalidated automatically but on one button click event i have stopped timer before 10 sec complete,but when again i come to that view it creates two timer and the call method twice and for third it called same method 3 times
DecilneTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(MakeSound) userInfo:nil repeats:YES];
and i have invalidate that like this on button click event
[DecilneTimer invalidate];
DecilneTimer=nil;
how to solve duplicate creation of timer???

You should use:
self.DecilneTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(MakeSound) userInfo:nil repeats:YES];
Using self.propertyName = ... you are using the property accessor and retaining the NSTimer automatically so that it doesnt get created again and again. And what you were doing was simply changing the ivar value directly

Related

Creating multiple NSTimers for future update

I want my user to be able to tap a button and set a timer for a future date. If they tap the button multiple times I want to set multiple future events. What I have so far is this:
- (IBAction)setFutureButtonTapped:(id)sender {
NSTimer *futureTimer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:#selector(futureMethod) userInfo:nil repeats:NO];
}
This causes futureMethod to be called once in 30 seconds, however, if I press the button once, then again in 10 seconds, I would like future method to be called at time 30, and time 40. How do I achieve this?

WebService Call after timeInterval

I want to my Webservice invoke once every 6 Hours.I am newer in iOS. Please help any help would be apperciated.I am stuck.
You can user NSTimer and schedule it for 6 hours
NSTimer *timer = nil;
timer = [NSTimer scheduledTimerWithTimeInterval:6*60*60 //6 hour
target:self
selector:#selector(performAction)
userInfo:nil
repeats:YES];
you can call using NSTimer. this is automatically called which time you set.
e.g.
NSTimer *timer= [NSTimer scheduledTimerWithTimeInterval:10.0(your time) target:self selector:#selector(someMethod) userInfo:nil repeats:YES];
-(void)someMethod
{
////API called here...
}
In ViewDidLoad
NSTimer *timer = [NSTimer timerWithTimeInterval:360.0 target:self selector:#selector(hideandview) userInfo:nil repeats:YES];
-(void)hideandview
{
////API called here...
}
You should save the last time in prefence (NSUserDefaults) when you make a call.
Whenever the app starts. Start timer and check current time with last saved time and get the difference.

Multiple timers set un in the same variable

Is this code a problem?
self.UpdateTimer = [NSTimer scheduledTimerWithTimeInterval:60. target:self selector:#selector(update:) userInfo:nil repeats:YES];
I notice that this is in a method that is called often. However there is only one place where the timer is invalidated and set to nil ... the dealloc of that class.
Does setting a new timer multiple times automatically invalidate the old timer?
No, setting the timer to a new object does not invalidate the previous timer. You will end up with multiple timers running if you call that line of code multiple times, but only invalidate the currently referenced timer in one place elsewhere.
When you create the timer, you should probably check if one is already set and invalidate it before creating a new one.
if( self.UpdateTimer )
{
[self.UpdateTimer invalidate];
self.UpdateTimer = nil;
}
self.UpdateTimer = [NSTimer scheduledTimerWithTimeInterval:60. target:self selector:#selector(update:) userInfo:nil repeats:YES];
OR, simply leave the original one running, if you don't need to reset the timer interval.
if( !self.UpdateTimer )
{
self.UpdateTimer = [NSTimer scheduledTimerWithTimeInterval:60. target:self selector:#selector(update:) userInfo:nil repeats:YES];
}

How to stop NSTimer at a specific point?

I've created a simple button game that gives the user a point with every tap of the button. The button randomly appears on screen every 1.5 seconds. I want the game to end after 30 seconds or after 20 random button pop ups. I've been using the code below to have the button randomly pop-up on the screen:
timer = [NSTimer scheduledTimerWithTimeInterval: 1.5 target:self
selector:#selector(moveButton:)
userInfo:nil
repeats:YES];
I've declared the timer in the header file:
NSTimer *timer;
#property (nonatomic, retain) NSTimer *timer;
I've read Apple Docs on Using Timers but fail to fully understand it. I thought maybe I could use:
- (void)countedTimerFireMethod:(NSTimer *)timer{
count ++;
if(count > 20){
[self.timer invalidate];
self.timer = nil;
But it does not work properly. What am I doing wrong? I'm new to objective-C so I'm not that familiar with how things work.
The problem is on your timer method you are passing moveButton method but in below method where you are stopping the timer that method name is different so try this:-
self.timer = [NSTimer
scheduledTimerWithTimeInterval: 1.5 target:self
selector:#selector(moveButton:)
userInfo:nil
repeats:YES];
//just change the method name below
- (void)moveButton:(NSTimer *)timer{
count ++;
if(count > 20){
[self.timer invalidate];
self.timer = nil;}
If you are using new version of Xcode then you don not need to declare
NSTimer *timer;
and when scheduling a timer you can use
self.timer = [NSTimer scheduledTimerWithTimeInterval: 1.5 target:self
selector:#selector(moveButton:)
userInfo:nil
repeats:YES]
instead of
timer = [NSTimer scheduledTimerWithTimeInterval: 1.5 target:self
selector:#selector(moveButton:)
userInfo:nil
repeats:YES]
You are using correct method to stop the timer i.e invalidate
You can also refer the link for more clarification.
Please let me know if you solve this problem through the above code.

Weird behavior of NSTimer in class

I have a real weird behavior of NSTimer object.
I'm trying to call ChangePic method with repeat counts of 2 seconds, But the timer isn't repeating. and i have this problem only in 1 class in the project
I have 5 View Controllers in my App, and the same code works in all classes except this one.
Does anybody has idea how could it be? is it possible that something blocking the timer ?
-BTW, ChangePic Method is called only once, and not repeating.
My Code:
ViewController.h
#property (nonatomic, strong) NSTimer *timer;
ViewController.m
_timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(ChangePic) userInfo:nil repeats:YES];
[_timer fire];
-(void) ChangePic {
NSLog(#"testing");
}
Assign it to the property not the backing variable.
self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(ChangePic) userInfo:nil repeats:YES];
It's difficult to say exactly what your issue is given that there isn't a whole lot of background given. I can tell you that I answered another question where the problem turned out to be the fact that one of the views was blocking the timer. Timers can run on a number of different modes and different scenarios can cause the timer to sometimes be blocked. Try adding the timer to all common modes like this:
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
Also, like #Hejazi said, remove the 'fire' method call. It's not needed on a scheduled timer.

Resources