gonzalezreal / Groot

From JSON to Core Data and back.
Other
534 stars 61 forks source link

Update NSManagedObject with JSON #72

Open ManueGE opened 8 years ago

ManueGE commented 8 years ago

Hi,

In the projects where I use Groot, very often I need to update existing objects that has not (yet) a primary key using a given JSON.

As an example, let's think I can create a Character from my app and send to my server. The Character I created has not a primary key, because that key is provided by the server after being pushed. It would be great if once the Character is pushed I can use the response from my server to update it:

let character = Character(context: managedObjectContext)
character.name = "Batman"
character.realName = "Bruce Wayne"

character.pushToServer { json in // the json is: {"id": 1, "name": "Batman", "real_name": "Bruce Wayne"}     
    character.update(with: json)
    // now, character.id = 1 
}

I usually solve it by creating a NSManagedObject category that provides this functionality, basically exposing one private method. I wonder if it would make sense adding such functionality to the official release. If so, I could prepare a pull request.

Any thoughts?