title: Swift CoreData create or update entity from JSON
body: <p>Hi I'm working with <code>CoreData</code> Project and <code>JSON</code> response from server. My objective is create an <code>Entity</code> <code>failable</code> <code>initializer</code> from <code>json</code> parameter which will first check if <code>entity</code> already exist (by check property "id" compare to json["id] field. If entity already exist. the <code>initializer</code> will only update entity with <code>json</code>. If entity did not exist, the <code>initializer</code> will create a new entity and update property of new entity with the <code>json</code>.</p>
  
  <p>How can I create the failable initializer?</p>
  
  <p>I have tried this but don't know it is correct or not (Have some unwrap optional.</p>
  
  <pre><code>import SwiftyJSON
  import CoreData
  
  extension Movie {
      struct Keys {
          static let id = "id"
          static let title = "title"
      }
  
      convenience init?(in context: NSManagedObjectContext!, with json: JSON?) {
  
          self.init(entity: NSEntityDescription.entity(forEntityName: "Movie", in: context)!, insertInto: context)
          guard let json = json else {
              return nil
          }
          id = json[Keys.id].numberValue
          title = json[Keys.title].string
  
      }
  
      func update(from json: JSON?) {
          guard let json = json else {
              return
          }
  
          id = json[Keys.id].numberValue
          title = json[Keys.title].string
      }
  
  }
  </code></pre>