title: How to update the values in Core Data with the updated values coming from the same API in Swift3
body: <p>I have a public API which returns the response like this:</p>
  
  <pre><code>{
      "messages": null,
      "data": [
          {
              "id": 27,
              "key": "ABC",
              "value": "5",
              "description": "this is abc"
          },
          {
              "id": 28,
              "key": "DEF",
              "value": "1",
              "description": "THis is def"
          },
          {
              "id": 32,
              "key": "GHI",
              "value": "24",
              "description": "This is GHI"
          },
          {
              "id": 35,
              "key": "123",
              "value": "dd/MMM/yyyy",
              "description": "123"
          }]
   "status": 0
  }
  </code></pre>
  
  <p>These values I am storing in the Core Data. Next when I am calling this API by passing the Time Stamp I will receive the Updated values for keys like: </p>
  
  <pre><code>{
      "messages": null,
      "data": [
          {
              "id": 27,
              "key": "ABC",
              "value": "5",
              "description": "Hi all"
          },
          {
              "id": 28,
              "key": "DEF",
              "value": "1",
              "description": "I am here"
          }]
   "status": 0
  }
  </code></pre>
  
  <p>So if you see the above response the keys whose values have been changed are displayed. So I need to update the core data with the updated values corresponding to the Key.</p>
  
  <p>That means in my Core Data I need to update the values of ABC and DEF key.<br>
  <strong>How is it possible in Swift3?</strong></p>
  
  <p>The code which I used here for inserting datas:</p>
  
  <pre><code>func saveApplcationSettingsinCoredata(responseArray: [ApplicationSettingsDataResponse]) {
      for settingsObj in responseArray {
        if #available(iOS 10.0, *) {
          let context = CoreDataHandler.sharedInstance.persistentContainer.viewContext
          if let applicationSettingsEntity = NSEntityDescription.insertNewObject(forEntityName: "ApplicationSettings", into: context) as? ApplicationSettings {
            applicationSettingsEntity.keyDescription = settingsObj.description
            applicationSettingsEntity.key = settingsObj.key
            applicationSettingsEntity.value = settingsObj.value
            do {
              try context.save()
            } catch let error {
              print(error)
            }
  
          } else {
            // Fallback on earlier versions
          }
        } else {
          let context = CoreDataHandler.sharedInstance.managedObjectContext
          if let applicationSettingsEntity = NSEntityDescription.insertNewObject(forEntityName: "ApplicationSettings", into: context) as? ApplicationSettings {
            applicationSettingsEntity.keyDescription = settingsObj.description
            applicationSettingsEntity.key = settingsObj.key
            applicationSettingsEntity.value = settingsObj.value
            do {
              try context.save()
            } catch let error {
              print(error)
            }
  
          } else {
            // Fallback on earlier versions
          }
        }
      }
    }
  
    func getSettingsDataFor(key: String) -&gt; [ApplicationSettings] {
      var fetchedObjects: [ApplicationSettings] = []
      if #available(iOS 10.0, *) {
        do {
          let context = persistentContainer.viewContext
          let fetchRequest = NSFetchRequest&lt;NSFetchRequestResult&gt;(entityName: "ApplicationSettings")
          fetchRequest.fetchLimit = 1
          fetchRequest.predicate = NSPredicate(format: "key == %@", key)
          try fetchedObjects = (context.fetch(fetchRequest) as? [ApplicationSettings])!
        } catch {
        }
        return fetchedObjects
      } else {
        do {
          let context = managedObjectContext
          let fetchRequest = NSFetchRequest&lt;NSFetchRequestResult&gt;(entityName: "ApplicationSettings")
          fetchRequest.fetchLimit = 1
          fetchRequest.predicate = NSPredicate(format: "key == %@", key)
          try fetchedObjects = (context.fetch(fetchRequest) as? [ApplicationSettings])!
        } catch {
        }
        return fetchedObjects
      }
    }
  </code></pre>
  
  <p>The code where I call this:</p>
  
  <pre><code>import UIKit
  
  enum ApplicationSettingsKey {
    case ABC
    case DEF
    case GHI
    case JKL
  
  
    var key: String {
      switch self {
      case .ABC:
        return keyValue(key: "ABC")
      case .DEF:
        return keyValue(key: "DEF")
      case .GHI:
        return keyValue(key: "GHI")
      case .JKL:
        return keyValue(key: "JKL")
      }
    }
    func keyValue(key: String) -&gt; String {
      if #available(iOS 10.0, *) {
        let object = CoreDataHandler.sharedInstance.getSettingsDataFor(key: key)
        let value = object[0].value ?? "Printing default value. No value retrieved from db"
        return value
      } else {
        // Fallback on earlier versions
      }
      return ""
    }
  
  }
  </code></pre>