Reputation: 6822
I am doing some research into using Core Location. I would like to save location information via Core Data. So far it seems like I can simply save a CLLocation information (this is essentially the smallest location information). Is this CLLocation all that is required to later restore the data and determine if the user is at/near that particular location?
Upvotes: 0
Views: 408
Reputation: 1424
I'm doing this by saving the latitude, longitude, and elevation information as Doubles (NSNumbers) in a waypoint record in a CoreData database and then using them to create a CLLocationCoordinate2D point whenever I load them back from the database.
You'll want to do a bit of filtering on the way in to verify you have a good point (horizontal accuracy below a threshold - and above 0 since they can flag bad points with negative values). I've also seen out-of-sequence data that's problematic so I keep track of the timestamp of the latest value loaded, and discard any new points with a timestamp before that value.
There are issues to work through to deal with background mode and LocationManager settings, but a structure capturing locations into CoreData can work well - I've captured tracks to the database with over 20,000 waypoints over the course of hours without problems.
Upvotes: 1
Reputation: 70936
Since CLLocation
conforms to NSCoding
, it could be as simple as creating a Core Data attribute with the "transformable" type. You could then assign CLLocation
objects to this attribute and read them back.
However you would not be able to filter or sort the data using this value-- for example if you want to find all instances with a location in a specific area. For that you'd need to add attributes to represent the CLLocation
latitude and longitude values separately from the CLLocation
itself. You might also want altitude and/or accuracy info, depending on what kind of fetching you expect to do.
Upvotes: 1