-
Notifications
You must be signed in to change notification settings - Fork 395
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
RFC: Swift Codable Attribute Support #375
Comments
I can't speak to the Swift devil-in-the-details, but this addition would fit in very well with mogenerator's general thrust of generating obvious code for obvious situations to lubricate Core Data's gears. |
On vacation atm, so not sure am I missing something obvious...
|
Hi @radianttap. It's binary because that's how the built-in coders work and because binary is a type that Core Data understands. |
Commit 28c0a72 includes an implementation of this. |
Like I said previously, I use CoreDataRepresentable protocol to map custom enums and structs into Core Data supported types. That works rather well in the last year or so. I never used 'binary' type for the attribute thus when you said it can't be used in predicates it rang a pretty big bell. I went through the largest project I'm maintaining to see just how much I use these attributes in predicates. Ends up – a lot. Most use cases for custom types is to map enums which represent attributes used for filtering. Thus I have a lot of predicates using exactly those types
Hence is not an option for me to lose this. (I'll need to go over other proposed changes in 376, 377) |
Hey @radianttap, I'm giving |
Feature Request
I think it would be extremely useful if Swift developers could use Codable attributes in Core Data.
Core Data has long supported transformable properties, which work transparently with any type that conforms to
NSCoding
. SupportingCodable
is conceptually the same thing: values are stored in a binary form and converted to/from a deserialized value as needed. Core Data doesn't have support for this (see rdar://37708071 if you can), but in the meantime we can do something about it.Note that this issue is only about
Codable
for attributes. ImplementingCodable
for managed object subclasses is a very different question, addressed in #377The Plan
I'm working on the following scheme and would appreciate any feedback people might have.
Declare an attribute as "binary".
In the attribute's
userInfo
, include a new key to be namedattributeCodableTypeName
. The value is any Swift type name where the type conforms toCodable
.mogenerator
generates custom accessor code for the property that converts to/from theCodable
type. The code can use any valid binary encoding so long as it's the same going in and coming out. I'm testing this withJSONEncoder
/JSONDecoder
but the property list versions would work too.For example, given the following Swift type:
One could add a binary attribute
type
to an entity, and includeattributeCodableTypeName
=EventType
in user info. The generated code for this property would be:[Note that in this example,
Event.Attributes
is defined as astruct
as in the current swift42 branch, thanks to PR #368.]With this change, values of
EventType
can be assigned to thetype
attribute in the same manner as tranformables are now.Limitation
This might be too similar to transformables, in that it would be generally impossible to use values of these attributes in predicates. Usually that's not an issue, but with Codables it might be more problematic. In this example, for instance, there's no support for fetching all objects with a given
EventType
value.I'd really like to get Codable support in, though, so any comments pro or con are appreciated.
The text was updated successfully, but these errors were encountered: