ManagedModels icon indicating copy to clipboard operation
ManagedModels copied to clipboard

Can we flatten structs?

Open helje5 opened this issue 2 years ago • 1 comments

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 avatar Oct 24 '23 12:10 helje5

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.

helje5 avatar Oct 24 '23 14:10 helje5