What's the correct way to store class instance (e.g. MKMapItem) in core data? (swiftUI)

Viewed 178

Short version

How could I store the value of class instance(for example: MKMapItem) into core data?

Long version:

I need to present/store following info after the user select location:

   * <MKMapItem>.name: String
   * <MKMapItem>.placemark.title: String
   * <MKMapItem>.placemark.coordinate: CLLocationCoordinate2D
  1. In the beginning, I plan to store coordinate, and read other info by MKPlacemark(coordinate: coordinate). Unfortunately it has never been able to find placemark from coordinate.

  2. Thus, I change my plan to store the whole MKMapItem instance. I supposed I can use JSON to encode and store as Binary Data. However, in such way, I need to duplicate the whole MKMapItem structure and conform each to Codable.

Thus I wonder if there is a better way to store it ?

  1. My backup plan is to create an entity to store name:String, title:String, latitude:Double, longitude: Double in CoreData, but then I lost the MKMapItem class structure.

But I feel there might be a much better way to do it...


p.s. I started learning iOS Dev from SwiftUI time. Thus I do not have much pre-knowledge for codes in MVC times or earlier.

0 Answers
Related