I have an NSTimer which fires on the current date at a specific time set by the user. For example 12:00 PM at 2012-06-22. When the user clicks a button it triggers the timer if the hour is equal to 12:00 PM. It is ok, but after this time passed the NSTimer triggers again when the user click the button. I do not want such behavior, so how can I do so the timer does not fire when the fire date has passed?
NSTimer *t = [[NSTimer alloc] initWithFireDate:fireDate
interval:0.0
target:self
selector:#selector(doTimerAction:)
userInfo:nil repeats:NO];
NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:t forMode: NSDefaultRunLoopMode];
[t release];
Are you sure you need NSTimer for this, providing I've understood you couldn't you just do
-(IBAction)btnPush:(id)sender{
if (hourInput == currentHour){
[self performSelector:#selector(doAction)];
}
Hope this helps if no leave me a comment and ill try to help more.
Related
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.
This question already has answers here:
NSTimer not firing
(3 answers)
Closed 8 years ago.
My timer only fires once even though I have repeat initialized as YES. I want my timer to fire every 6 seconds? What is the problem?
#interface TCAMyScene (){
NSTimer *bombTimer;
}
bombTimer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:6 target:self selector:#selector(setBomb) userInfo:nil repeats:YES];
[bombTimer fire];
Solution:
bombTime = [NSTimer scheduledTimerWithTimeInterval:6 target:self selector:#selector(setBomb) userInfo:nil repeats:YES];
From the documentation for NSTimer:
You must add the new timer to a run loop, using addTimer:forMode:.
Upon firing, the timer sends the message aSelector to target. (If the
timer is configured to repeat, there is no need to subsequently re-add
the timer to the run loop.)
You're better calling:
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
As this will add it to the current run loop in the default mode (which is usually what you want, unless there's an explicit need for a different run loop or mode)
I use NSTimer to periodically download data in the background, when wifi is enabled.
However there is also the option to press a button that downloads the data manually. Therefore it is possible to enable and disable the autoupdater.
When a period is "interrupted" by diabling the autoupdate, I want to schedule the next update when the remaining time of this period after the point of time when enabling it is over.
Because NSTimer has no pause method, I use NSTimer's initWithFireDate method.
timer = [[NSTimer alloc] initWithFireDate:fireDate interval:updateFrequency target:self selector:#selector(timerTick:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
the fireDate is definitely after the given date:
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:remainingWaitingTime];
despite that,the timer is scheduled after the timer's initialization. I am disabling the timer and calculating the remaining time the following way:
NSDate *lastFireDate = [timer fireDate];
NSDate *current = [NSDate date];
[timer invalidate];
double timeSinceLastFire = [current timeIntervalSinceDate:lastFireDate];
remainingWaitingTime = updateFrequency - timeSinceLastFire;
The calculated time (remainingWaitingTime) has a correct value.
What am I doing wrong?
Best Regards,
Fabian
You have to add your timer to the run loop by doing the following:
[[NSRunLoop currentRunLoop] addTimer: timer forMode: NSDefaultRunLoopMode];
I have an NSTimer
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(periodicTimer)
userInfo:nil
repeats:YES];
which does
- (void)periodicTimer
{
NSLog(#"Bang!");
if (timerStart != nil)
[timerLabel setText:[[NSDate date] timeDifference:timerStart]];
}
The problem is that while scrolling a tableview (or doing other tasks) the label doesn't get updated, furthermore, "Bang!" doesn't appear, so I supposed the method doesn't get called.
My question is how to update the label periodically even when the user is playing around with the app interface.
You'll need to add your timer to the UITrackingRunLoopMode to make sure your timer also fires during scrolling.
NSRunLoop *runloop = [NSRunLoop currentRunLoop];
NSTimer *timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:#selector(myTimerAction:) userInfo:nil repeats:YES];
[runloop addTimer:timer forMode:NSRunLoopCommonModes];
[runloop addTimer:timer forMode:UITrackingRunLoopMode];
From:
https://stackoverflow.com/a/1997018/474896
Not sure about this one, but my first guess would be that the main thread on which the interface is being rendered your timer just doesn't get a chance to do anything while its updating the interface.
You could create a new thread with a new run loop for your timer, but that is a bit of an ugly solution maybe. What functionality in your app are you trying to achieve? Maybe we can advise a better strategy than using a timer.
I'm new to working with timers on the iPhone and would need some support.
I have a method as below that takes the time and update a UILabel in my userinterface. I also have an NSTimer that calls that method once a second (I only show hours and minutes). This works just fine, except for the first second the App is live (as I understand it takes one second before the timer calls the method).
I would like to call my method from viewDidLoad, but how can I supply the right arguments? Or is there a better way of doing this?
// Timer for updating time
[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:#selector(updateTime:)
userInfo:nil
repeats:YES];
-(void)updateTime: (NSTimer *) timer {
currentTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTimeString = [dateFormatter stringFromDate:currentTime];
[dateFormatter release];
timeLabel.text = currentTimeString;
}
Appreciate anything that would point me in the right direction.
In your method, you don't use the timer variable right? So, why just call [self updateTime:nil]. That should work
One more option ... you can do:
[NSTimer scheduledTimerWithTimeInterval:0.01f
target:self
selector:#selector(updateTime:)
userInfo:nil
repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:#selector(updateTime:)
userInfo:nil
repeats: YES];