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)
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.
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.
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.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
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)
}
`
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.
@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 withEncodablebecause theinit(decoder:)needs to be implemented, whileNSManagedObject'sinit(context:)needs an instance of theNSManagedObjectContextwhere the object will be added. (Basically you can callinit(decoder:)many times without side effects, butinit(context:)does)You are hitting the same limitation with
CoreStoreObjecthere, where it needs the context (via theNSManagedObjectininit(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.@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.