mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-11 20:00:30 +01:00
How to do a many to many/through relationship? #404
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @pejrich on GitHub (Feb 19, 2023).
Here is an example of how I would think to set this up.
But when I do this, the
Field.Virtualclosure gets the errorType of expression is ambiguous without more context. And if I set a variable toobject.$postLikes.value, it shows<<type error>>rather than what i'd expect being[PostLike]. Am I doing something wrong?@JohnEstropia commented on GitHub (Feb 20, 2023):
That's not supposed to work, at the very least I did not design

Virtualfields to be used for relationships. There is currently no subscript forObjectProxythat allows relationship access:The reason for this is because we will hit problems when working with
ObjectSnapshots, which only copiesStored,Virtual, andCodedfields. Relationship access can't be thread-safe as they need to be fetched from particular contexts.What I would suggest is to make
likersaRelationshipfield and sync its value withpostLikeswhenever it gets updated. If that sounds like a maintenance burden, then an extension onPostshould be sufficient as well:(I typed this by hand so it may not compile as-is, but I hope this gives you an idea)
Understandably, relationship-dependent computed fields are some of the use cases for Core Data's derived attributes and so far I haven't had support for it in CoreStore. But even then, Core Data's derived attributes support still only supports aggregated values such as sums and counts, and not collections of objects. If there's a large demand for it I'll try thinking of an elegant way to implement it, but so far extensions handle this use case well.