-
-
Notifications
You must be signed in to change notification settings - Fork 255
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
How to do a many to many/through relationship? #470
Comments
That's not supposed to work, at the very least I did not design The reason for this is because we will hit problems when working with What I would suggest is to make extension Post {
func likers() -> [User] {
return self.postLikes.compactMap({ $0.user })
}
}
extension ObjectSnapshot where O == Post {
func likers() -> [ObjectSnapshot<User>]? {
// this cannot be access from a different context the ObjectSnapshot was created from
return self.$postLikes?.compactMap({ $0.user.asSnapshot() })
}
func likers(in dataStack: DataStack) -> [ObjectSnapshot<User>]? {
// note that if this object is already deleted, relationships cannot be accessed even if the fields are available within the snapshot, thus the Optional return value
return self.asReadOnly(in: dataStack)?.postLikes.compactMap({ $0.user.asSnapshot(in: dataStack) })
}
func likers(in transaction: BaseDataTransaction) -> [ObjectSnapshot<User>]? {
// Same here
return self.asReadOnly(in: transaction)?.postLikes.compactMap({ $0.user.asSnapshot(in: transaction) })
}
} (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. |
Here is an example of how I would think to set this up.
But when I do this, the
Field.Virtual
closure 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?The text was updated successfully, but these errors were encountered: