Issue with timer - ios

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

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?

Periodic sending small data to server in background mode

I am implementing a kind of tracking app and i need to spot my location once and send it once per 10 - 20 seconds (period value is important and can't be exceeded).
To lower battery consumption i stop location updates. This works good in foreground, but how can i do it when app moved in background?
I looked info about background fetch, but it hasn't got precise time for periodic sending data
How can i perform this task?
You can start and Stop periodic location update while app is in background.
To achieve this add class from given link for Location Update.
After that import LocationTracker.h in your AppDelegate.
Add Below code in your didFinishLaunchingWithOptions.
let locationTracker : LocationTracker = LocationTracker();
locationTracker?.startLocationTracking();
In LocationTracker.m, you can set duration to restart update.Here i set 1 minute or 60 Seconds.
//Restart the locationMaanger after 1 minute
self.shareModel.timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self
selector:#selector(restartLocationUpdates)
userInfo:nil
repeats:NO];
You can also set duration time for fetch Locations. Here I fetch location for 10 Seconds.
self.shareModel.delay10Seconds = [NSTimer scheduledTimerWithTimeInterval:10 target:self
selector:#selector(stopLocationDelayBy10Seconds)
userInfo:nil
repeats:NO];

how to hit the service url everyday 7 am using NStimer in IOS?

- (NSTimer *) timer
{
(!_timer)
{
_timer = [NSTimer timerWithTimeInterval:86400 target:self selector:#selector(timeset:) userInfo:nil repeats:YES];
}
return _timer;
}
You can't schedule execution for a specific time in the background on iOS. You can schedule a UILocalNotification - but your app will only be launched if the user taps on the notification.
Your best bet is to use background fetch mode. You app will be woken at intervals and given an opportunity to fetch new data. You can check to see if the time is at or after 7 and decide whether to refresh the data.

Displaying Game Time in Milliseconds Xcode

So I am fooling around with some apps and I am trying to get a game timer to be in milliseconds instead of just the normal 1,2,3. I want it to be in a format similar to this 1.000,2.000. Or even 1.00,2.00.
Here is my code:
(void)startTimer {
if (count == 30) {
// 3
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target: self
selector:#selector(ElapsedTime)
userInfo:nil
repeats:YES];
Record the start time with
NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
Any time you want to see how much time has elapsed, use:
NSTimeInterval elapsed = [NSDate timeIntervalSinceReferenceDate] -
start;
elapsed will be in seconds. If you want milliseconds, subtract the whole number to get the decimal part. Multiply the decimal part by 1000 to get milliseconds.
If you want your time to change more often than once a second, make the time interval of your timer less than 1.0 seconds. Note that timers aren't accurate for more than about 1/50 of a second however.

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