Saving Swift CLLocation in CoreData

后端 未结 3 777
梦毁少年i
梦毁少年i 2020-12-29 01:11

I am trying to save CLLocation data in Core Data. My property is set as a Transformable object.

Some sample code out there indicates one can save the lo

相关标签:
3条回答
  • 2020-12-29 01:22

    You will have much less trouble when you create a NSManagedObject subclass for the CLLocation objects. Then create methods for storing and retrieving for convenience:

    import Foundation
    import CoreData
    import CoreLocation
    
    class LocationPoint: NSManagedObject {
    
        @NSManaged var latitude: NSNumber!
        @NSManaged var longitude: NSNumber!
        @NSManaged var altitude: NSNumber!
        @NSManaged var timestamp: NSDate!
        @NSManaged var horizontalAccuracy: NSNumber
        @NSManaged var verticalAccuracy: NSNumber
        @NSManaged var speed: NSNumber
        @NSManaged var course: NSNumber
    
        func initFromLocation(location: CLLocation) {
            self.latitude           = location.coordinate.latitude
            self.longitude          = location.coordinate.longitude
            self.altitude           = location.altitude
            self.timestamp          = location.timestamp
    
            self.horizontalAccuracy = location.horizontalAccuracy > 0.0 ? location.horizontalAccuracy : 0.0
            self.verticalAccuracy   = location.verticalAccuracy > 0.0 ? location.verticalAccuracy : 0.0
            self.speed              = location.speed > 0.0 ? location.speed : 0.0
            self.course             = location.course > 0.0 ? location.course : 0.0
        }
    
        func location() -> CLLocation {
            return CLLocation(
                coordinate: CLLocationCoordinate2D(latitude: self.latitude.doubleValue, longitude: self.longitude.doubleValue),
                altitude: self.altitude.doubleValue,
                horizontalAccuracy: self.horizontalAccuracy.doubleValue,
                verticalAccuracy: self.verticalAccuracy.doubleValue,
                course: self.course.doubleValue,
                speed: self.speed.doubleValue,
                timestamp: self.timestamp
            )
        }
    

    You have to set up your data model with an entity according to this class.

    0 讨论(0)
  • 2020-12-29 01:28

    What you are missing is that NSValue is for storing types, i.e., contiguous blocks of memory, not objects. CLLocation is an object.

    In order to store a CLLocation in core data, you will have to store each of the properties. An alternative is to use a Coder.

    0 讨论(0)
  • 2020-12-29 01:36

    This post is still helpful but a bit outdated.

    To save CLLocation I use:

    let clocation: [CLLocation]
    
    let archivedLocation = NSKeyedArchiver.archivedData(withRootObject: clocation)
    

    And to Fetch with Core Data:

    let unarchivedLocation = NSKeyedUnarchiver.unarchiveObject(with: (coreDataLocation.value(forKey: "location") as! NSData) as Data) as! CLLocation)
    
    0 讨论(0)
提交回复
热议问题