Skip to content

Commit 1697dc7

Browse files
authoredJan 11, 2020
Merge pull request #152 from merlos/fix/151
Fixes #151 - crash on renaming waypoints.
2 parents 876e999 + 157bb8e commit 1697dc7

File tree

3 files changed

+88
-12
lines changed

3 files changed

+88
-12
lines changed
 

‎OpenGpxTracker/CoreDataHelper.swift

+84-9
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ class CoreDataHelper {
481481

482482

483483
/// Delete all trackpoints and waypoints in Core Data.
484-
func deleteAllPointsFromCoreData() {
484+
func deleteAllTrackFromCoreData() {
485485

486486
let privateManagedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
487487
privateManagedObjectContext.parent = appDelegate.managedObjectContext
@@ -490,18 +490,14 @@ class CoreDataHelper {
490490

491491
// Creates fetch requests for both trackpoint and waypoint
492492
let trackpointFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "CDTrackpoint")
493-
let waypointFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "CDWaypoint")
494493

495494
if #available(iOS 9.0, *) {
496495
privateManagedObjectContext.perform {
497496
do {
498497
let trackpointDeleteRequest = NSBatchDeleteRequest(fetchRequest: trackpointFetchRequest)
499-
let waypointDeleteRequest = NSBatchDeleteRequest(fetchRequest: waypointFetchRequest)
500498

501499
// execute both delete requests.
502500
try privateManagedObjectContext.execute(trackpointDeleteRequest)
503-
try privateManagedObjectContext.execute(waypointDeleteRequest)
504-
505501
try privateManagedObjectContext.save()
506502

507503
self.appDelegate.managedObjectContext.performAndWait {
@@ -530,6 +526,64 @@ class CoreDataHelper {
530526
}
531527
}
532528

529+
do {
530+
// Executes all delete requests
531+
try privateManagedObjectContext.execute(trackpointAsynchronousFetchRequest)
532+
try privateManagedObjectContext.save()
533+
self.appDelegate.managedObjectContext.performAndWait {
534+
do {
535+
// Saves the changes from the child to the main context to be applied properly
536+
try self.appDelegate.managedObjectContext.save()
537+
} catch {
538+
print("Failure to save context after delete: \(error)")
539+
}
540+
}
541+
542+
} catch let error {
543+
print("NSAsynchronousFetchRequest (for batch delete <iOS 9) error: \(error)")
544+
}
545+
}
546+
}
547+
548+
/// Delete all trackpoints and waypoints in Core Data.
549+
func deleteAllWaypointsFromCoreData() {
550+
551+
let privateManagedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
552+
privateManagedObjectContext.parent = appDelegate.managedObjectContext
553+
554+
print("Core Data Helper: Batch Delete trackpoints and waypoints from Core Data")
555+
556+
// Creates fetch requests for both trackpoint and waypoint
557+
let waypointFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "CDWaypoint")
558+
559+
if #available(iOS 9.0, *) {
560+
privateManagedObjectContext.perform {
561+
do {
562+
let waypointDeleteRequest = NSBatchDeleteRequest(fetchRequest: waypointFetchRequest)
563+
564+
// execute both delete requests.
565+
try privateManagedObjectContext.execute(waypointDeleteRequest)
566+
567+
try privateManagedObjectContext.save()
568+
569+
self.appDelegate.managedObjectContext.performAndWait {
570+
do {
571+
// Saves the changes from the child to the main context to be applied properly
572+
try self.appDelegate.managedObjectContext.save()
573+
} catch {
574+
print("Failure to save context after delete: \(error)")
575+
}
576+
}
577+
}
578+
catch {
579+
print("Failed to delete all from core data, error: \(error)")
580+
}
581+
582+
}
583+
584+
}
585+
else { // for pre iOS 9 (less efficient, load in memory before removal)
586+
533587
let waypointAsynchronousFetchRequest = NSAsynchronousFetchRequest(fetchRequest: waypointFetchRequest) { asynchronousFetchResult in
534588

535589
guard let results = asynchronousFetchResult.finalResult as? [CDWaypoint] else { return }
@@ -543,7 +597,6 @@ class CoreDataHelper {
543597

544598
do {
545599
// Executes all delete requests
546-
try privateManagedObjectContext.execute(trackpointAsynchronousFetchRequest)
547600
try privateManagedObjectContext.execute(waypointAsynchronousFetchRequest)
548601
try privateManagedObjectContext.save()
549602
self.appDelegate.managedObjectContext.performAndWait {
@@ -600,16 +653,21 @@ class CoreDataHelper {
600653
// if gpx is saved, but further trkpts are added after save, and crashed, trkpt are appended, not adding to new trkseg.
601654
root.tracks.last?.tracksegments[Int(self.lastTracksegmentId)].add(trackpoints: self.tracksegments.first!.trackpoints)
602655
self.tracksegments.remove(at: 0)
656+
603657

604658
}
605659
else {
606660
track.tracksegments = self.tracksegments
607661
root.add(track: track)
662+
//root.waypoints = self.waypoints
663+
//root.add(waypoints: self.waypoints)
608664
}
609-
root.waypoints = [GPXWaypoint]()
610-
root.add(waypoints: self.waypoints)
665+
//root.waypoints = [GPXWaypoint]()
666+
//root.add(waypoints: self.waypoints)
667+
root.waypoints = self.waypoints
611668
// asks user on what to do with recovered data
612669
DispatchQueue.main.sync {
670+
print(root.gpx())
613671
// main action sheet setup
614672
let alertController = UIAlertController(title: NSLocalizedString("CONTINUE_SESSION_TITLE", comment: "no comment"), message: NSLocalizedString("CONTINUE_SESSION_MESSAGE", comment: "no comment"), preferredStyle: .actionSheet)
615673

@@ -690,10 +748,27 @@ class CoreDataHelper {
690748
self.currentSegment = GPXTrackSegment()
691749
}
692750

751+
func clearAllExceptWaypoints() {
752+
// once file recovery is completed, Core Data stored items are deleted.
753+
self.deleteAllTrackFromCoreData()
754+
755+
// once file recovery is completed, arrays are cleared.
756+
self.tracksegments = []
757+
self.currentSegment = GPXTrackSegment()
758+
759+
// current segment should be 'reset' as well
760+
self.currentSegment = GPXTrackSegment()
761+
762+
// reset order sorting ids
763+
self.trackpointId = 0
764+
self.tracksegmentId = 0
765+
}
766+
693767
/// clears all
694768
func clearAll() {
695769
// once file recovery is completed, Core Data stored items are deleted.
696-
self.deleteAllPointsFromCoreData()
770+
self.deleteAllTrackFromCoreData()
771+
self.deleteAllWaypointsFromCoreData()
697772

698773
// once file recovery is completed, arrays are cleared.
699774
self.clearObjects()

‎OpenGpxTracker/GPXSession.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class GPXSession {
157157
self.tracks = gpx.tracks
158158

159159
// remove last track as that track is packaged by Core Data, but should its tracksegments should be seperated, into self.tracksegments.
160-
self.tracks.removeLast()
160+
//self.tracks.removeLast()
161161

162162
self.trackSegments = lastTrack.tracksegments
163163

‎OpenGpxTracker/ViewController.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,8 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
907907
print("viewController:: applicationWillTerminate")
908908
GPXFileManager.removeTemporaryFiles()
909909
if gpxTrackingStatus == .notStarted {
910-
map.coreDataHelper.deleteAllPointsFromCoreData()
910+
map.coreDataHelper.deleteAllTrackFromCoreData()
911+
map.coreDataHelper.deleteAllWaypointsFromCoreData()
911912
}
912913
}
913914

@@ -1125,7 +1126,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
11251126
GPXFileManager.save(filename!, gpxContents: gpxString)
11261127
self.lastGpxFilename = filename!
11271128
self.map.coreDataHelper.deleteCDRootFromCoreData()
1128-
self.map.coreDataHelper.clearAll()
1129+
self.map.coreDataHelper.clearAllExceptWaypoints()
11291130
self.map.coreDataHelper.add(toCoreData: filename!, willContinueAfterSave: true)
11301131
}
11311132
let cancelAction = UIAlertAction(title: NSLocalizedString("CANCEL", comment: "no comment"), style: .cancel) { (action) in }

0 commit comments

Comments
 (0)
Failed to load comments.