CoreDataHelper

class CoreDataHelper

Core Data implementation. As all Core Data related logic is contained here, I considered it as a helper.

Implementation learnt / inspired from 4 part series: https://marcosantadev.com/coredata_crud_concurrency_swift_1/

IDs

  • for waypoints

    Declaration

    Swift

    var waypointId: Int64
  • for trackpoints

    Declaration

    Swift

    var trackpointId: Int64
  • id to seperate trackpoints in different tracksegements

    Declaration

    Swift

    var tracksegmentId: Int64
  • Declaration

    Swift

    var isContinued: Bool
  • Declaration

    Swift

    var lastTracksegmentId: Int64

Other Declarations

Add to Core Data

  • Adds the last file name to Core Data

    Declaration

    Swift

    func add(toCoreData lastFileName: String, willContinueAfterSave willContinue: Bool)

    Parameters

    lastFileName

    Last file name of the previously logged GPX file.

  • Adds a trackpoint to Core Data

    A track segment ID should also be provided, such that trackpoints would be seperated in their track segments when recovered.

    Declaration

    Swift

    func add(toCoreData trackpoint: GPXTrackPoint, withTrackSegmentID Id: Int)

    Parameters

    trackpoint

    the trackpoint meant to be added to Core Data

    Id

    track segment ID that the trackpoint originally was in.

  • Adds a waypoint to Core Data

    Declaration

    Swift

    func add(toCoreData waypoint: GPXWaypoint)

    Parameters

    waypoint

    the waypoint meant to be added to Core Data

Update Core Data

  • Updates a previously added waypoint to Core Data

    The waypoint at the given index will be updated accordingly.

    Declaration

    Swift

    func update(toCoreData updatedWaypoint: GPXWaypoint, from index: Int)

    Parameters

    updatedWaypoint

    the waypoint meant to replace a already added, Core Data waypoint.

    index

    the waypoint that is meant to be replaced/updated to newer data.

Retrieval From Core Data

  • Retrieves everything from Core Data

    Currently, it retrieves CDTrackpoint, CDWaypoint and CDRoot, to process from those Core Data types to CoreGPX types such as GPXTrackPoint, GPXWaypoint, etc.

    It will also call on crashFileRecovery() method to continue the next procudure.

    Declaration

    Swift

    func retrieveFromCoreData()

Delete from Core Data

  • Delete Waypoint from index

    Declaration

    Swift

    func deleteWaypoint(fromCoreDataAt index: Int)

    Parameters

    index

    index of the waypoint that is meant to be deleted.

  • Delete all objects of entity given as parameter in Core Data.

    Declaration

    Swift

    func coreDataDeleteAll<T>(of type: T.Type) where T : NSManagedObject

Handles recovered data

  • Prompts user on what to do with recovered data

    Adds all the ‘recovered’ content retrieved earlier to newly initialized GPXRoot. Deletes and clears core data stuff after user decision is made.

    Currently, there are three user decisions allowed:

    • To continue last session, which loads the recovered data including previous file data (if applicable) on the map.
    • To save recovered data silently in background and start a fresh new session immediately.
    • To delete and ignore recovered data, to start a fresh new session instead.

    Declaration

    Swift

    func crashFileRecovery()
  • saves recovered data to a gpx file, silently, without loading on map.

    Declaration

    Swift

    func saveFile(from gpx: GPXRoot, andIfAvailable lastfileName: String)

Reset & Clear

  • Resets trackpoints and waypoints Id

    the Id is to ensure that when retrieving the entities, the order remains. This is important to ensure that the resulting recovery file has the correct order.

    Declaration

    Swift

    func resetIds()
  • Clear all arrays and current segment after recovery.

    Declaration

    Swift

    func clearObjects()
  • Declaration

    Swift

    func clearAllExceptWaypoints()
  • clears all

    Declaration

    Swift

    func clearAll()
  • Declaration

    Swift

    func modernBatchDelete<T>(of type: T.Type) where T : NSManagedObject
  • Declaration

    Swift

    func legacyBatchDelete<T>(of type: T.Type) where T : NSManagedObject
  • Declaration

    Swift

    func rootFetchRequest() -> NSAsynchronousFetchRequest<CDRoot>
  • Declaration

    Swift

    func trackPointFetchRequest() -> NSAsynchronousFetchRequest<CDTrackpoint>
  • Declaration

    Swift

    func waypointFetchRequest() -> NSAsynchronousFetchRequest<CDWaypoint>