-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use JSON in Core Data #31
Comments
To be honest, I have never used Core Data. This feels like one of the Swift ↔︎ Objective-C interoperability pain points. Would it be acceptable for you to introduce a second, serializable version of the problematic property? class Person: NSManagedObject {
var metaData: JSON?
@NSManaged private var encodedMetaData: Data?
} And then you could keep those two in sync using |
just to say I'm interested in this issue as well. My current hack is to store my JSON as a String in core data. Then do a conversion like this back to generic JSON:
|
Personally I’d use an |
In my specific case, I have a lot of different types of JSON objects (sometimes of unknown structure). So I do not want to again build a large library of classes to convert json to object class and then back again to json. It is much easier for me to use GenericJSON and keep the objects as JSON. So I was looking for a way to store that in core data. One way is to use Data type, at the moment I just use String as a temporary fix. I was looking for a better more principled way to store JSON in core data. |
I stand by my comment… The If you’re not sure what I mean, I might be able to provide you w/ a code example. |
yes please, it would be great if you could provide some code. Note I know nothing about Objective-c. |
Core Data Interop
How should
JSON
properties be used in conjunction with Core Data?Discussion
JSON
neatly solves the case where a Swift type needs to be created from a freeform json string (i.e. the json schema is unknown at compile time and subject to change). Furthermore, it allows the json to modelled in a type safe way.Codable
conformance allows the type to be easily converted to and fromData
. This makes persisting the type to disk as data relatively simple.However using the
JSON
type in the context of Core Data poses challenges.JSON
was designed as a pure Swift type whereas Core Data has is origin in Objective-C.The issue is that using a property of type
JSON
on anNSManagedObject
subclass is not permitted. The compiler produces an error:This is expected since Core Data does not support
JSON
out of the box, and it turns out, themetaData
attribute cannot be marked asTransformable
since Swift Enums with associated values (whichJSON
is built upon) cannot be represented in Objective-C and so conformance toNSCoding
seems unlikely.By contrast Swift dictionaries, say of type
[String: Any]
can be used on anNSManagedObject
subclass directly since they conform toNSCoding
and so their corresponding model attribute can be marked asTransformable
.Options
How then should properties of type
JSON
be used in the context of Core Data? Or, does Core Data's tight integration with Objective-C mean theJSON
type cannot be used?Some options which come to mind:
NSCoding
conformance toJSON
allowing the attribute's type to beTransformable
.JSON
does not derive fromNSObject
.toDictionary()
andfromDictionary
methods facilitating[String: Any]
transformations.Transformable
with a custom setter and getter in theNSManagedObject
's subclass to do the transformation.Are they other ways to do this? (I am relatively new to Core Data).
Example Object
Here the
metaData
property needs to hold and store flexible json which makesJSON
a good candidate.The text was updated successfully, but these errors were encountered: