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

Add relationships by merging with existing ones #73

Open
Heia- opened this issue Oct 13, 2016 · 2 comments
Open

Add relationships by merging with existing ones #73

Heia- opened this issue Oct 13, 2016 · 2 comments

Comments

@Heia-
Copy link

Heia- commented Oct 13, 2016

This is more of a feature request than a bug, and is the only thing preventing Groot from doing exactly what my App needs. As it stands all relationships are treated as complete/perfect relationships, which is not always the case.

Feature:
Be able to set an annotation on a relationship that flags it to not override existing relationships but instead merge them.

For example, my situation requires that certain objects be attached to the current logged in User object. These objects may already be attached to another User and as such need to retain that relationship post mapping.

I have been trying to work on my own fork to get this to work, and so far have had no success.

@ManueGE
Copy link

ManueGE commented Oct 13, 2016

Not sure I understand you, but I handled with a similar issue (I think) a few days ago.

I had a Entity User which has some attributes. In the responses from my api, I had an array of attributes which were different for each request, but they all belonged to the user so I didn't want them to replace the existing ones.

Finally I solved it by adding a custom accessor for my attributes relationship:

extension User {

    public var attributes: Set<Attribute>? {
        get {
            self.willAccessValue(forKey: #keyPath(User.attributes))
            let attributes = self.primitiveValue(forKey: #keyPath(User.attributes)) as? Set<Attribute>
            self.didAccessValue(forKey: #keyPath(User.attributes))
            return attributes
        }

        set {

            var set = Set<Attribute>()

            for attr in (attributes ?? []) {
                set.insert(attr)
            }

            for attr in (newValue ?? []) {
                set.insert(attr)
            }

            self.willChangeValue(forKey: #keyPath(User.attributes))
            self.setPrimitiveValue(set, forKey: #keyPath(User.attributes))
            self.didChangeValue(forKey: #keyPath(User.attributes))
        }
    }
}

Again, not sure if this is the problem you have, but maybe it could help.

@Heia-
Copy link
Author

Heia- commented Oct 14, 2016

Thanks for the suggestion! I was able to use it to get something to work but not quite as generic/universal as I was hoping for.

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

2 participants