-1

I have an array of type 'CLLocationCoordinate2D'. For example, this is how my data looks like:

[__C.CLLocationCoordinate2D(latitude: 60.24971706534604, longitude: 13.748807609081268), __C.CLLocationCoordinate2D(latitude: 60.2463096103141, longitude: 14.025525748729706), __C.CLLocationCoordinate2D(latitude: 60.190718087002963, longitude: 13.781766593456268)]

How could I save this to Core Data?

konyv12
  • 716
  • 2
  • 8
  • 23
  • Possible duplicate of [How can I store a specific format array in Core Data?](http://stackoverflow.com/questions/43293084/how-can-i-store-a-specific-format-array-in-core-data) – pbasdf Apr 08 '17 at 14:20
  • That's mine thread actually, but this time I am asking specifically about this. – konyv12 Apr 08 '17 at 14:22

3 Answers3

2

You'll want to save your coordinates as a Double in your object model. From there, when you need a coordinate, you can create a method in an extension for your NSManagedObject containing the X and Y coordinates that returns a CLLocation object:

func location() -> CLLocation {
    return CLLocation(latitude: self.coordinateX, longitude: self.coordinateY)
}
Adrian
  • 16,233
  • 18
  • 112
  • 180
  • but it will still be a CLLocation type, that can't be saved as Double? – konyv12 Apr 08 '17 at 14:32
  • I don't think you can save a CLLocationObject in an NSManagedObject, but you can attach a Double to managed object. So you'd just save the parameter that you need for a CLLocation in your managed object...that is a Double. Then, when you need a CLLocation, you'd just use `myManagedObject.location()` to get it – Adrian Apr 08 '17 at 14:34
  • In that case, you'd just need to save the X and Y coordinates separately in your managed object. When you need to retrieve them, you'd just call the location() method. – Adrian Apr 08 '17 at 14:36
2

If you are using these coordinates for a pathing purposes (coordinates in a path), string-encoding the array could be a really good way to go. This might be too complicated to implement yourself, but I suggest taking a look at Google's Encoded Polylines.

The idea there is that you have an array of coordinates (lat/long), and it converts the entire array to one single string.

If by any chance you are using GoogleMaps in your project, that would be the easiest, by far. Let's say you create a GMSPath-object like this:

let path = GMSMutablePath()
path.add(CLLocationCoordinate2D)
path.add(CLLocationCoordinate2D)
path.add(CLLocationCoordinate2D)

Then simply do path.encodedPath() to get a string representation of the path.

In their example, these points: (38.5, -120.2), (40.7, -120.95), (43.252, -126.453) are turned into this single string:

_p~iF~ps|U_ulLnnqC_mqNvxq`@

If you store this string in your database, and want to extract them again, you can simply say

let path = GMSPath(fromEncodedPath: encodedString)

This is really good for storing in DB, or sending over API. Then again, it might not be fit for your use.

Community
  • 1
  • 1
Sti
  • 8,275
  • 9
  • 62
  • 124
0

Two ways to handle this:

  1. Best way is to use NSEncoder mechanism and then store the object as Core data item.
  2. Another way is to use the Coredata paramter datatype as transformable and store the object directly as CoreLocation. It works.

I prefer the first way as it will let you read the object and print as and where required while debugging.

codelover
  • 1,113
  • 10
  • 28