Save complex JSON to Core Data in Swift

前端 未结 3 1073
名媛妹妹
名媛妹妹 2020-12-20 10:16

I want to save the JSON Result from web service to Core data, following is the code for JSON parsing.

if let jsonResult = try JSONSerialization.jsonObject(wi         


        
3条回答
  •  抹茶落季
    2020-12-20 10:17

    It's quite easy to decode the JSON directly into Core Data with Decodable

    • First of all create extensions of CodingUserInfoKey and JSONDecoder to be able to pass the managed object context

      extension CodingUserInfoKey {
          static let context = CodingUserInfoKey(rawValue: "context")!
      }
      
      extension JSONDecoder {
          convenience init(context: NSManagedObjectContext) {
              self.init()
              self.userInfo[.context] = context
          }
      }
      
    • Add conformance to Decodable in both classes

      class Name: NSManagedObject, Decodable {
      
      class ParamDownload: NSManagedObject, Decodable {
      
    • In the class Name (not the extension) add

      private enum CodingKeys: String, CodingKey { case name, iin, isOn }
      
      required convenience init(from decoder: Decoder) throws {
          guard let context = decoder.userInfo[.context] as? NSManagedObjectContext else { fatalError("NSManagedObjectContext is missing") }
          let entity = NSEntityDescription.entity(forEntityName: "Name", in: context)!
          self.init(entity: entity, insertInto: context)
          let values = try decoder.container(keyedBy: CodingKeys.self)
          name = try values.decode(String.self, forKey: .name)
          iin = try values.decode(String.self.self, forKey: .iin)
          isOn = try values.decode(Bool.self.self, forKey: .isOn)
      }
      
    • In the class ParamDownload (not the extension) add

      private enum CodingKeys: String, CodingKey { case code = "Code", desc = "Desc", names = "iinData" }
      
      required convenience init(from decoder: Decoder) throws {
          guard let context = decoder.userInfo[.context] as? NSManagedObjectContext else { fatalError("NSManagedObjectContext is missing") }
          let entity = NSEntityDescription.entity(forEntityName: "ParamDownload", in: context)!
          self.init(entity: entity, insertInto: context)
          let values = try decoder.container(keyedBy: CodingKeys.self)
          code = try values.decode(String.self, forKey: .code)
          desc = try values.decode(String.self, forKey: .desc)
          names = try values.decode(Set.self, forKey: .names)
          names.forEach { $0.pd = self }
      }
      

    To decode the JSON create the Decoder with the convenience initializer

    let decoder = JSONDecoder(context: CoreDataStack.sharedInstance.persistentContainer.viewContext)
    

    the init methods handle the relationships.

    I recommend to declare the Core Data attributes as much non-optional as possible.
    If an attribute must be remain optional replace decode with decodeIfPresent.

提交回复
热议问题