Decodable CoreStoreObject subclass implementation #239

Closed
opened 2025-12-29 15:27:12 +01:00 by adam · 2 comments
Owner

Originally created by @eraydiler on GitHub (Oct 29, 2018).

Hello,

I'm trying to figure out how to implement a CoreStoreObject subclass that also needs to conform to Decodable protocol. What I came up with is the code below.

`
class TestObject: CoreStoreObject, Decodable {
let title = Value.Optional("title", initial: nil)

 private enum CodingKeys: String, CodingKey {
     case title = "title"
 }

 required init(from decoder: Decoder) throws {
     let values = try decoder.container(keyedBy: CodingKeys.self)

     title.value = try values.decodeIfPresent(String.self, forKey: .title)

     super.init(asMeta: ())
 }

 required init(asMeta: Void) {
     super.init(asMeta: ())
 }

 required init(rawObject: NSManagedObject) {
     super.init(asMeta: ())
 }

}
`

However I'm having error;
[CoreStore: Assertion Failure] Value.swift:373 value
↪︎ Attempted to access values from a 'Test' meta object. Meta objects are only used for querying keyPaths and infering types.

I think issue is related to the init(asMeta:...), init(rawObject:...) methods' implementation.
What is the proper way to implement these methods?
Thanks.

Originally created by @eraydiler on GitHub (Oct 29, 2018). Hello, I'm trying to figure out how to implement a CoreStoreObject subclass that also needs to conform to Decodable protocol. What I came up with is the code below. ` class TestObject: CoreStoreObject, Decodable { let title = Value.Optional<String>("title", initial: nil) private enum CodingKeys: String, CodingKey { case title = "title" } required init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) title.value = try values.decodeIfPresent(String.self, forKey: .title) super.init(asMeta: ()) } required init(asMeta: Void) { super.init(asMeta: ()) } required init(rawObject: NSManagedObject) { super.init(asMeta: ()) } } ` However I'm having error; ❗ [CoreStore: Assertion Failure] Value.swift:373 value ↪︎ Attempted to access values from a 'Test' meta object. Meta objects are only used for querying keyPaths and infering types. I think issue is related to the `init(asMeta:...), init(rawObject:...)` methods' implementation. What is the proper way to implement these methods? Thanks.
adam closed this issue 2025-12-29 15:27:12 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Oct 30, 2018):

As mentioned somewhere in the discussion in mogenerator's prototype for Codable support, NSManagedObjects generally do not really play well with Encodable because the init(decoder:) needs to be implemented, while NSManagedObject's init(context:) needs an instance of the NSManagedObjectContext where the object will be added. (Basically you can call init(decoder:) many times without side effects, but init(context:) does)

You are hitting the same limitation with CoreStoreObject here, where it needs the context (via the NSManagedObject in init(rawObject:)).

You can borrow the techniques used in the mogenerator proposal above, but at that point I would recommend you to use ImportableUniqueObjects instead as they are much much more simple.

@JohnEstropia commented on GitHub (Oct 30, 2018): As mentioned somewhere in the discussion in [mogenerator's prototype for Codable support](https://github.com/rentzsch/mogenerator/issues/377), `NSManagedObject`s generally do not really play well with `Encodable` because the `init(decoder:)` needs to be implemented, while `NSManagedObject`'s `init(context:)` needs an instance of the `NSManagedObjectContext` where the object will be added. (Basically you can call `init(decoder:)` many times without side effects, but `init(context:)` does) You are hitting the same limitation with `CoreStoreObject` here, where it needs the context (via the `NSManagedObject` in `init(rawObject:)`). You can borrow the techniques used in the mogenerator proposal above, but at that point I would recommend you to use `ImportableUniqueObject`s instead as they are much much more simple.
Author
Owner

@eraydiler commented on GitHub (Oct 30, 2018):

I solved my issue by creating two different objects. They conform CoreStoreObject and Decodable seperately. Added additional methods to make required mapping between them. Thanks for the information.

@eraydiler commented on GitHub (Oct 30, 2018): I solved my issue by creating two different objects. They conform CoreStoreObject and Decodable seperately. Added additional methods to make required mapping between them. Thanks for the information.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#239