Skip to content
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

Can we flatten structs? #24

Open
helje5 opened this issue Oct 24, 2023 · 1 comment
Open

Can we flatten structs? #24

helje5 opened this issue Oct 24, 2023 · 1 comment

Comments

@helje5
Copy link
Contributor

helje5 commented Oct 24, 2023

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
Copy link
Contributor Author

helje5 commented Oct 24, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant