Skip to content

Instantly share code, notes, and snippets.

@odrobnik
Created July 13, 2012 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odrobnik/3104860 to your computer and use it in GitHub Desktop.
Save odrobnik/3104860 to your computer and use it in GitHub Desktop.
Code where the crash occurred
- (void)recordLocation:(CLLocation *)location
{
if (previousRecordedLocation)
{
NSTimeInterval interval = [location.timestamp timeIntervalSinceDate:previousRecordedLocation.timestamp];
NSTimeInterval distance = [location distanceFromLocation:previousRecordedLocation];
CLLocationDistance currentSpeed = distance/interval;
if (previousRecordedSpeed)
{
CLLocationDistance speedDelta = (currentSpeed - previousRecordedSpeed)/interval;
if (fabs(speedDelta)>=4.0 || currentSpeed > 120)
{
// ignoring implausible speed change
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"FilterSignalDips"])
{
return;
}
}
}
previousRecordedSpeed = currentSpeed;
}
[previousRecordedLocation release];
previousRecordedLocation = [location retain];
// make new trackpoint
TrackPoint *newTrackPoint = (id)[NSEntityDescription insertNewObjectForEntityForName:@"TrackPoint" inManagedObjectContext:self.managedObjectContext];
newTrackPoint.latitude = [NSNumber numberWithDouble:location.coordinate.latitude];
newTrackPoint.longitude = [NSNumber numberWithDouble:location.coordinate.longitude];
newTrackPoint.horizontalAccuracy = [NSNumber numberWithDouble:location.horizontalAccuracy];
newTrackPoint.timestamp = location.timestamp;
if (location.verticalAccuracy>0)
{
newTrackPoint.altitude = [NSNumber numberWithDouble:location.altitude];
newTrackPoint.verticalAccuracy = [NSNumber numberWithDouble:location.verticalAccuracy];
}
if (!trackCurrentlyRecording)
{
// make new track
Track *newTrack = (id)[NSEntityDescription insertNewObjectForEntityForName:@"Track" inManagedObjectContext:self.managedObjectContext];
newTrack.name = [self.dateTitleFormatter stringFromDate:location.timestamp];
newTrack.distance = [NSNumber numberWithDouble:0];
newTrack.beginTimestamp = location.timestamp;
self.trackCurrentlyRecording = newTrack;
}
// add to current recording track
[trackCurrentlyRecording addTrackPointsObject:newTrackPoint];
if (trackCurrentlyRecording.mostRecentTrackPoint)
{
CLLocationDistance currentDistance = [trackCurrentlyRecording.distance doubleValue];
CLLocationDistance addedDistance = [[trackCurrentlyRecording.mostRecentTrackPoint location] distanceFromLocation:location];
currentDistance += addedDistance;
trackCurrentlyRecording.distance = [NSNumber numberWithDouble:currentDistance];
}
trackCurrentlyRecording.mostRecentTrackPoint = newTrackPoint;
[[NSNotificationCenter defaultCenter] postNotificationName:@"RecordedTrackPoint" object:newTrackPoint userInfo:nil];
NSError *error;
if (![self.managedObjectContext save:&error])
{
NSLog(@"Error saving MOC: %@", [error localizedDescription]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment