title: NSManagedObject and Codable for class stored in server and local storage
body: <p>I am creating a class that represents data that is either in local storage or comes in from online. On runtime I will use a get request to get an array of these objects from a server and compare them to the existing ones in CoreData in order to determine a new set of objects to be used for the app and to be stored.</p>
  
  <p>As such I have created a CoreData managed class that represents my object as such it inherits <code>NSManagedObject</code>. This will allow me to deal with the local storage getting the array in storage and putting back a new one after the app has finished.</p>
  
  <p>I would then like to make this class Codable that way I can implement the <code>required public init(from decoder: Decoder)</code> method that allows me to get my array of objects like this in the get request. <code>JSONDecoder().decode([DataClass].self, from: data!)</code>.</p>
  
  <p>There are two ways I am trying to do this and neither of them are working.</p>
  
  <p><strong>Problems:</strong></p>
  
  <p>I have a <code>private enum CodingKeys: String, CodingKey {...}</code> in class scope and using that to encode works great but for whatever reason that triggers an error that says that the class doesn't implement encode which I have no intention of implementing. I am not sure why that matters.</p>
  
  <p>I can put the enum inside the scope of the initializer but if I do that a new problem pops up. I must call a designated initializer of <code>NSManagedObject</code>. This is a problem because that call requires a <code>NSManagedObjectContext</code> which I dont have.</p>
  
  <p><strong>As an alternative:</strong></p>
  
  <p>I have create a method <code>func getDataClass(from decoder: Decoder) -&gt; DataClass?</code> however this also brings with it two problems. I still must initialize a <code>DataClass</code> object somehow and I have no clue how to bring that into the <code>JSONDecoder().decode([DataClass].self, from: data!)</code> line.</p>