58

Codable seems a very exciting feature. But I wonder how we can use it in Core Data? In particular, is it possible to directly encode/decode a JSON from/to a NSManagedObject?

I tried a very simple example:

enter image description here

and defined Foo myself:

import CoreData

@objc(Foo)
public class Foo: NSManagedObject, Codable {}

But when using it like this:

let json = """
{
    "name": "foo",
    "bars": [{
        "name": "bar1",
    }], [{
        "name": "bar2"
    }]
}
""".data(using: .utf8)!
let decoder = JSONDecoder()
let foo = try! decoder.decode(Foo.self, from: json)
print(foo)

The compiler failed with this errror:

super.init isn't called on all paths before returning from initializer

and the target file was the file that defined Foo

I guess I probably did it wrong, since I didn't even pass a NSManagedObjectContext, but I have no idea where to stick it.

Does Core Data support Codable?

1
  • A good example which uses the accepted answer can be found here
    – mfaani
    Dec 31, 2018 at 18:18

4 Answers 4

102
+100

You can use the Codable interface with CoreData objects to encode and decode data, however it's not as automatic as when used with plain old swift objects. Here's how you can implement JSON Decoding directly with Core Data objects:

First, you make your object implement Codable. This interface must be defined on the object, and not in an extension. You can also define your Coding Keys in this class.

class MyManagedObject: NSManagedObject, Codable {
    @NSManaged var property: String?

    enum CodingKeys: String, CodingKey {
       case property = "json_key"
    }
}

Next, you can define the init method. This must also be defined in the class method because the init method is required by the Decodable protocol.

required convenience init(from decoder: Decoder) throws {
}

However, the proper initializer for use with managed objects is:

NSManagedObject.init(entity: NSEntityDescription, into context: NSManagedObjectContext)

So, the secret here is to use the userInfo dictionary to pass in the proper context object into the initializer. To do this, you'll need to extend the CodingUserInfoKey struct with a new key:

extension CodingUserInfoKey {
   static let context = CodingUserInfoKey(rawValue: "context")
}

Now, you can just as the decoder for the context:

required convenience init(from decoder: Decoder) throws {

    guard let context = decoder.userInfo[CodingUserInfoKey.context!] as? NSManagedObjectContext else { fatalError() }
    guard let entity = NSEntityDescription.entity(forEntityName: "MyManagedObject", in: context) else { fatalError() }

    self.init(entity: entity, in: context)

    let container = decoder.container(keyedBy: CodingKeys.self)
    self.property = container.decodeIfPresent(String.self, forKey: .property)
}

Now, when you set up the decoding for Managed Objects, you'll need to pass along the proper context object:

let data = //raw json data in Data object
let context = persistentContainer.newBackgroundContext()
let decoder = JSONDecoder()
decoder.userInfo[.context] = context

_ = try decoder.decode(MyManagedObject.self, from: data) //we'll get the value from another context using a fetch request later...

try context.save() //make sure to save your data once decoding is complete

To encode data, you'll need to do something similar using the encode protocol function.

14
  • 15
    Great idea. Is there any way to initialize and then update existing objects this way? For example check if the id is already in CoreData. If it exists load the object and update, otherwise create new (as described above).
    – 1b0t
    Dec 18, 2017 at 21:51
  • 1
    I'd add that since decoding values from JSON can throw, this code potentially allows objects to be inserted in the context even if JSON decoding encountered an error. You could catch this from the calling code and handle it by deleting the just-inserted-but-throwing object. Jan 15, 2018 at 16:51
  • 1
    Hey i followed the same steps as above but the context.hasChanges always gives me false even if the the managedobject has values after decoding. Since there are no changes context.save is not saving. I tried to call context.save directly, i passes with no error but database is empty. I also checked the pointer of the context that was passed to decoder and it is same. Any idea?
    – Tarang
    Feb 6, 2018 at 13:29
  • 1
    @Tarang Did you manage to make it work? I have the same problem here, database is empty, context is not persisted.
    – Akhu
    May 21, 2018 at 9:33
  • 3
    This answer doesn't help if you want to update an existing object. It always creates new object and duplicates your existing records. Jun 17, 2018 at 3:44
13

CoreData is its own persistence framework and, per its thorough documentation, you must use its designated initializers and follow a rather specific path to creating and storing objects with it.

You can still use Codable with it in limited ways just as you can use NSCoding, however.

One way is to decode an object (or a struct) with either of these protocols and transfer its properties into a new NSManagedObject instance you've created per Core Data's docs.

Another way (which is very common) is to use one of the protocols only for a non-standard object you want to store in a managed object's properties. By "non-standard", I mean anything thst doesn't conform to Core Data's standard attribute types as specified in your model. For example, NSColor can't be stored directly as a Managed Object property since it's not one of the basic attribute types CD supports. Instead, you can use NSKeyedArchiver to serialize the color into an NSData instance and store it as a Data property in the Managed Object. Reverse this process with NSKeyedUnarchiver. That's simplistic and there is a much better way to do this with Core Data (see Transient Attributes) but it illustrates my point.

You could also conceivably adopt Encodable (one of the two protocols that compose Codable - can you guess the name of the other?) to convert a Managed Object instance directly to JSON for sharing but you'd have to specify coding keys and your own custom encode implementation since it won't be auto-synthesized by the compiler with custom coding keys. In this case you'd want to specify only the keys (properties) you want to be included.

Hope this helps.

19
  • Thanks for the detailed explanation. I'm currently using the first approach as you mentioned. But I really hope NSManagedObject can conform to Codable by default, and there are methods like json = encoder.encode(foo) to encode it directly, and foo = decoder.decode(Foo.self, json, context) to decode directly. Hope to see it in an update or in the next major release.
    – hgl
    Jun 10, 2017 at 2:21
  • 2
    I really wouldn't count on it. The ability to customize the encoding/decoding pretty much covers all the bases for data transfer between your app's store and the majority of the real-world cases with the JSON de/coder alone. Since the two approaches are mutually exclusive for app persistence (due to their radically different design approaches and use cases) there's somewhere around zero chance of such support. But hope springs eternal. ;-) Jun 10, 2017 at 4:35
  • 1
    @JoshuaNozzi I totally disagree with this comment. You can change the mappings fairly easily and people use libraries for this specific approach. I wouldn't be surprised if support came in 2 or so iterations of iOS in the future. It would just require a tweak to the protocol to support population without initialization, or base level conformance with CoreData's current initialization interfaces and Codable enum code generation for CoreData models (which they already have code generation). The approaches aren't mutually exclusive and 99% of apps using core data are mapping JSON. Aug 15, 2017 at 0:06
  • @TheCodingArt To what are you referring? Custom store types? That’s a bit different to directly using Codable/Decodable directly on individual managed objects apart from the Core Data machinery. Aug 15, 2017 at 0:17
  • @JoshuaNozzi I never referenced anything about custom store types.... This is a simple serialization/deserialization mapping of properties in Swift with the Codable Code-generated key values. Aug 15, 2017 at 0:18
7

Swift 4.2:

Following casademora's solution,

guard let context = decoder.userInfo[.context] as? NSManagedObjectContext else { fatalError() }

should be

guard let context = decoder.userInfo[CodingUserInfoKey.context!] as? NSManagedObjectContext else { fatalError() }.

This prevents errors that Xcode falsely recognizes as array slice problems.

Edit: Use implicitly unwrapped optionals to remove the need to force unwrap .context every time it is being used.

4
  • I would rather make the static constant (.context) force unwrapped at the definition instead of sprinkling it all over the source like this.
    – casademora
    Sep 25, 2018 at 23:37
  • @casademora this is the same as your answer, just for swift 4.2 (EDIT: I see what you mean. Implicitly unwrapped optionals :-). ). Sep 27, 2018 at 0:10
  • 2
    Yeah, I’m aware of the difference. I’m just suggesting putting the unwrapping on the constant (in one place) as opposed to the userInfo accessor (potentially everywhere)
    – casademora
    Sep 27, 2018 at 4:29
  • Hi, I am encoding (in iOS app) and decoding (in watch kit extension), how did you manage to get the same context there?
    – Hemang
    Oct 16, 2018 at 4:26
5

As an alternative for those who would like to make use of XCode's modern approach to NSManagedObject file generation, I have created a DecoderWrapper class to expose a Decoder object which I then use within my object which conforms to a JSONDecoding protocol:

class DecoderWrapper: Decodable {

    let decoder:Decoder

    required init(from decoder:Decoder) throws {

        self.decoder = decoder
    }
}

protocol JSONDecoding {
     func decodeWith(_ decoder: Decoder) throws
}

extension JSONDecoding where Self:NSManagedObject {

    func decode(json:[String:Any]) throws {

        let data = try JSONSerialization.data(withJSONObject: json, options: [])
        let wrapper = try JSONDecoder().decode(DecoderWrapper.self, from: data)
        try decodeWith(wrapper.decoder)
    }
}

extension MyCoreDataClass: JSONDecoding {

    enum CodingKeys: String, CodingKey {
        case name // For example
    }

    func decodeWith(_ decoder: Decoder) throws {

        let container = try decoder.container(keyedBy: CodingKeys.self)

        self.name = try container.decode(String.self, forKey: .name)
    }
}

This is probably only useful for models without any non-optional attributes, but it solves my problem of wanting to use Decodable but also manage relationships and persistence with Core Data without having to manually create all my classes / properties.

Edit: Example of it in use

If I have a json object:

let myjson = [ "name" : "Something" ]

I create the object in Core Data (force cast here for brevity):

let myObject = NSEntityDescription.insertNewObject(forEntityName: "MyCoreDataClass", into: myContext) as! MyCoreDataClass

And I use the extension to have the object decode the json:

do {
    try myObject.decode(json: myjson)
}
catch {
    // handle any error
}

Now myObject.name is "Something"

2
  • If we have a Custom object like @NSManaged public var products: NSSet?. How will we decode this object. Dec 2, 2019 at 13:00
  • You can cast it to a regular set which is codable
    – devjme
    Jan 11, 2021 at 20:23

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.