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 ability to get a dictionary of only the values which have changed #62

Open
designatednerd opened this issue Nov 10, 2015 · 2 comments

Comments

@designatednerd
Copy link
Contributor

In instances where a PATCH endpoint requires only the changes be sent, it would be helpful to have a convenience method to get a dictionary of only those values which have changed.

Here's how I had this at least quasi-working in Swift, assuming the MOC hasn't been saved:

func dictionaryOfChangedValues() -> [String : AnyObject] {
    let changedValuesFromCoreData = self.changedValues()
    let classMapper = VOKCoreDataManager.sharedInstance().mapperForClass(User.self)
    let changedCoreDataKeys = changedValuesFromCoreData.keys
    let changedForeignKeys = changedCoreDataKeys.flatMap {
        key in
        return classMapper[key] as? String //
    }

    var returnDictionary = [String : AnyObject]()

    for (key, value) in self.vok_dictionaryRepresentation() {
        if changedForeignKeys.indexOf(key) != nil {
            returnDictionary[key] = value
        }
    }

    return returnDictionary
}
@designatednerd
Copy link
Contributor Author

Note that this is using the User.self method to grab the mapper because if I used self.dynamicType, I was getting not the User class but its parent class, which returned the wrong maps. I still have no idea why.

@brockboland
Copy link
Contributor

We'll probably need an equivalent (or maybe just an argument) that makes use of vok_dictionaryRepresentationRespectingKeyPaths

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

No branches or pull requests

2 participants