Support for @propertyWrappers for CoreStoreObject properties #302

Closed
opened 2025-12-29 18:25:28 +01:00 by adam · 1 comment
Owner

Originally created by @JohnEstropia on GitHub (Jan 10, 2020).

Originally assigned to: @JohnEstropia on GitHub.

In a future update, these CoreStoreObject properties

  • Value.Required, Value.Optional
  • Transformable.Required, Transformable.Optional
  • Relationship.ToOne, Relationship.ToManyOrdered, Relationship.ToManyUnordered

will be deprecated in favor of these @propertyWrapper implementations:

  • Field.Stored (replacement for Value.Required andValue.Optional)
  • Field.Virtual (new, equivalent to transient versions of Value.Required andValue.Optional)
  • Field.Coded (replacement for Transformable.Required andTransformable.Optional, with additional support for custom encoders such as JSON)
  • Field.Identifier (new)
  • Field.Relationship (replacement for Relationship.ToOne, Relationship.ToManyOrdered, and Relationship.ToManyUnordered)

Field types that are marked above as "replacement"s will be able to migrate directly from their old counterparts. So if you have a property declared now as

let title = Value.Optional<String?>("title", initial: "Mr.")

you can directly convert it to

@Field.Stored("title")
var title: String? = "Mr."

when you are ready. They will be effectively equal to Core Data, and your VersionLocks will remain intact.

Note that this effectively forces you to use a different syntax for queries:

  • Before: From<Person>.where(\.title == "Mr.")
  • After: From<Person>.where(\.$title == "Mr.")

Also a couple of advantages:

  • The @propertyWrapper versions will be magnitudes performant and efficient than their current implementations. Currently Mirror reflection is used a lot to inject the NSManagedObject reference into the properties. With @propertyWrappers this will be synthesized by the compiler for us. (See https://github.com/apple/swift/pull/25884)
  • The @propertyWrapper versions, being structs, will give the compiler a lot more room for optimizations which were not possible before due to the need for mutable classes.

The only disadvantage will be

  • You need to update your code by hand to migrate to the new @propertyWrappers
    (But the legacy ones will remain available for quite a while, so while it is recommended to migrate soon, no need to panic)

Post implementation ideas:

  • Blob support (background fetched)
  • Compound keys
  • Traits (ReadOnly, etc.)
Originally created by @JohnEstropia on GitHub (Jan 10, 2020). Originally assigned to: @JohnEstropia on GitHub. In a future update, these `CoreStoreObject` properties - `Value.Required`, `Value.Optional` - `Transformable.Required`, `Transformable.Optional` - `Relationship.ToOne`, `Relationship.ToManyOrdered`, `Relationship.ToManyUnordered` will be deprecated in favor of these `@propertyWrapper` implementations: - `Field.Stored` (replacement for `Value.Required` and`Value.Optional`) - `Field.Virtual` (new, equivalent to `transient` versions of `Value.Required` and`Value.Optional`) - `Field.Coded` (replacement for `Transformable.Required` and`Transformable.Optional`, with additional support for custom encoders such as JSON) - `Field.Identifier` (new) - `Field.Relationship` (replacement for `Relationship.ToOne`, `Relationship.ToManyOrdered`, and `Relationship.ToManyUnordered`) `Field` types that are marked above as "replacement"s will be able to migrate directly from their old counterparts. So if you have a property declared now as ```swift let title = Value.Optional<String?>("title", initial: "Mr.") ``` you can directly convert it to ```swift @Field.Stored("title") var title: String? = "Mr." ``` when you are ready. They will be effectively equal to Core Data, and your `VersionLock`s will remain intact. Note that this effectively forces you to use a different syntax for queries: - Before: `From<Person>.where(\.title == "Mr.")` - After: `From<Person>.where(\.$title == "Mr.")` Also a couple of advantages: - The `@propertyWrapper` versions will be magnitudes performant and efficient than their current implementations. Currently `Mirror` reflection is used a lot to inject the `NSManagedObject` reference into the properties. With `@propertyWrapper`s this will be synthesized by the compiler for us. (See https://github.com/apple/swift/pull/25884) - The `@propertyWrapper` versions, being `struct`s, will give the compiler a lot more room for optimizations which were not possible before due to the need for mutable classes. The only disadvantage will be - You need to update your code by hand to migrate to the new `@propertyWrapper`s (But the legacy ones will remain available for quite a while, so while it is recommended to migrate soon, no need to panic) Post implementation ideas: - Blob support (background fetched) - Compound keys - Traits (ReadOnly, etc.)
adam added the enhancementfixed labels 2025-12-29 18:25:28 +01:00
adam closed this issue 2025-12-29 18:25:28 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Mar 27, 2020):

Released now for version 7.1.0!
Please check the release notes: https://github.com/JohnEstropia/CoreStore/releases/tag/7.1.0

@JohnEstropia commented on GitHub (Mar 27, 2020): Released now for version `7.1.0`! Please check the release notes: https://github.com/JohnEstropia/CoreStore/releases/tag/7.1.0
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore-JohnEstropia#302