mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-11 20:00:30 +01:00
Decodable CoreStoreObject subclass implementation #239
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.