Creating multiple NSTimers for future update - ios

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?

Related

iOS Objective C progress bar update in background

I've run various types of code to try and make a progress bar increment for x seconds and continue to do so even when in the background. Currently the bar just resumes from where it left off, via the timer, when going from background to focus.
How can I continue the timer even when the user minimizes the app?
Declare a NSTimer property in .h. Then call it from anywhere like :
self.nstimer= [NSTimer scheduledTimerWithTimeInterval:0.10 self selector:#selector(updateTime:) userInfo:nil repeats:YES];
And inside the method (audiocontroller is another class property):
- (void)updateTime:(NSTimer *)timer
{
// Progress bar value
progressView.progress = ((float)recievedData / (float) xpectedTotalSize);
}
It will continue call the method even the app is in background. You can cancel this method calling from applicationDidBecomeActive() method.

ns timer duplicate created 2-3 times

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

NSTimer does not call selector after assigned time interval. iOS

I am having a banners array and displaying it in my custom cell. When user clicks on banner, I push a detailViewController and opens in-app browser.
I am changing banners after every 5 seconds. For that, I am using NSTimer to schedule the selector call. Everything works great.... Until, user clicks on banner and come back from detailViewController. When user comes back, NSTimer behaves really weird. It changes first banner after 5 seconds (as assigned) and then next banner is changed after 1 second and so on.
Her is the code I am using:
#pragma mark - User Methods
-(void) resetBannerRotationTimer {
[self.bannerTimer invalidate];
self.bannerTimer = nil;
self.timeInterval = 5.0f;
self.bannerTimer = [NSTimer scheduledTimerWithTimeInterval:self.timeInterval target:self selector:#selector(rotateBanner) userInfo:nil repeats:YES];
self.timeInterval = self.bannerTimer.timeInterval;
[[NSRunLoop currentRunLoop] addTimer:self.bannerTimer forMode:NSRunLoopCommonModes];
}
rotateBanner:
-(void) rotateBanner {
BannerCell *bannerCell = (BannerCell *)[self.dealsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
[bannerCell updateBanner];
}
In my updateBanner method, I am handeling UIPageControl to change pages. (I don't think that code needs to be posted).
I am calling resetBannerRotationTimer method in viewWillAppear method.
i think you are missing to fire your timer
add this line after timer scheduling
[self.bannerTimer fire];

Issue with timer

I have a driving application when I start the trip I have to collect location details along with date and time in hours, minutes, seconds and milliseconds for every second and accelerometer details along with date and time in hours, minutes, seconds and milliseconds for every 0.25 seconds and when the trip is stopped recording should be stopped. I have taken a timer for location details with interval 1 second
self.locationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(locationTimerFired:)
userInfo:nil
repeats:YES];
- (void) locationTimerFired:(NSTimer *)timer
{
CLLocation *newLocation = locationManager.location;
[self updateLocation:newLocation andSpeed:newLocation.speed];
[defaultCenter postNotificationName:LocationChangedNotification
object:nil
userInfo: userInfo];
}
in the received LocationChangedNotification I am recording the location and time as mentioned for every second.
This is sometime fine when the application is in foreground but when the application is in background timer timer fires interval is not accurate it fires sometimes for 2 seconds, for 3 seconds and some times 2 times a second why this is happening?
Please suggest. Also I have registered for location updates in background mode in info.plist.
I have taken a timer for accelerometer details with interval 0.25 second as follows
self.accelTimer = [NSTimer scheduledTimerWithTimeInterval:0.25
target:self
selector:#selector(timerFired:)
userInfo:nil
repeats:YES];
Thanks.
Don't use timers. Instead, configure the location manager to give you callbacks at your desired accuracy (presumably maximum) and use the callback to save the location and speed with the current time.
Then, when you come to use / display the data you can filter / extrapolate to get the location data at your desired time interval.
The timers don't work in the background because they aren't designed to. Location callbacks are designed to...

iOS Executing loop until certain condition is met of specified time has passed?

As the title states, i have a while loop that will be executed until certain condition is met, or until 5 seconds have passed.
What is the best way to solve this? I have seen some simple tutorial about NSTimer, but it seems to me that selector that is fired within NSTimer will be executed after time interval specified no matter what. I only need to execute it if condition is not met...
Just create an NSTimer scheduled action store the timer and if you reach your what you wanted to achieve deactivate this timer so that it doesn't trigger the action.
Basically:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(yourAction) userInfo:nil repeats:NO];
some code
//for deactivating the timer
[timer invalidate];
timer = nil;
You could start the NSTimer on the main thread (to ensure above code works) with this:
[self performSelectorOnMainThread:#selector(startTimerMethod) withObject:someOrNoObject waitUntilDone:NO];

Resources