Data-swift / ManagedModels

A SwiftData like `@Model` infrastructure for CoreData.
https://www.alwaysrightinstitute.com/managedmodels/
Apache License 2.0
98 stars 3 forks source link

Can we flatten structs? #24

Open helje5 opened 10 months ago

helje5 commented 10 months ago

Looks like SwiftData flattens Codable structure into own table columns vs storing them as JSON. Presumably to be able to run queries against such.

E.g.

@Model class Person {
  struct Address: Codable {
    var street: String
    var city: String
  }
  var privateAddress : Address
}

Even though the property is just one in the model, I think it ends up in separate columns in SQLite. Presumably to allow this:

#Predicate {
  $0.privateAddress.street = "XYZ"
}

Which seems valuable?

Not sure how we would hook that up in Core Data yet, I'd guess the Entity would need to get attributes for those.

helje5 commented 10 months ago

Actually that should be reasonable to do, a write to the address would just push down to the individual attributes. The reflection to destructure the structs in the macro might be fishy though. I guess it should work, if the structure is within the class itself.