Question: What's the general idea for *updating* MTLModels in a NSManagedObjectContext? #53

Closed
smithclay opened this Issue Jul 16, 2014 · 4 comments

Comments

Projects
None yet
2 participants

First of all, really liking Overcoat. The bundled TwitterTimeline example makes it very clear what the right way is to fetch data from an API. I'm unclear about what should happen when API clients need to update or delete remote objects, however.

When performing a PUT, POST or DELETE -- what's the general approach to use Overcoat with Mantle and CoreData?

Right now I have some pseudocode that looks like this (using a Car model with PromiseKit as an example).

// After a successful 'PUT' request, update the color attribute on the model.
Car *myCar; // a MTLModel
[overcoatClient updateCar:myCar withColor:@"red"].then(^(OVCResponse *result) {
  Car *updatedCar = (Car *)response.result;
  [myCar mergeValueForKey:@"color" fromModel:updatedCar];
  return updatedCar;
});

Then, I need to serialize the updated Car model to the NSManagedObjectContext:

  // Serialize the updated MTLModel to CoreData
  [Car managedObjectFromModel:updatedCar insertingIntoContext:context error:nil];

Is this the general idea, or are there already helpers/classes that make this a little easier? The two-step process (update MTLModel, then serialize to a NSManagedObjectContext) seems clunky and I might be missing something here.

Thanks!

Contributor

gonzalezreal commented Jul 18, 2014

Thanks for the feedback.

Regarding your question, I think the best solution is to use two model classes: Car and ManagedCar and perform the updates directly in ManagedCar which is the model that holds 'the truth' and must be in sync with the server. In the end, Car will only be used as an intermediary object for mapping the JSON returned by the server.

I think you touched one of the major issues with Mantle: direct JSON to NSManagedObject conversion is not supported. Everything must be done through MTLModel. This clearly has performance issues and one major issue:

  • You cannot apply partial updates coming from the server on your managed objects, every key not present in the JSON will nil the corresponding MTLModel property and in turn the corresponding NSManagedObject attribute or relationship.

I created my own JSON to NSManagedObject mapping solution for this reason, it's called Groot and I hope to integrate it with Overcoat soon.

On the other hand, Mantle 2.0 will drop Core Data support and presumably move it to a separate repository.

Ouch. The partial updates situation nil-ing out properties isn't great.

Groot, on the other hand, seems like a good thing for me to use -- especially for merging JSON into an existing NSManagedObject.

One thing to clarify, however -- using my example, would one approach creating a subclass of NSManagedObject called ManagedCar that then defines some methods on itself that uses Groot (or something else) to handle conversion from a JSON dictionary to the core data managed object?

{JSON} --> serialized to --> Car (MTLModel) --> used to update ManagedCar (subclass of NSManagedObject) --> updates/creates/deletes NSManagedObject state using Groot.
Contributor

gonzalezreal commented Jul 21, 2014

Why would you need Mantle in that case?

@gonzalezreal I wouldn't :) Thanks again for your help, will be taking a closer look at Groot.

smithclay closed this Jul 21, 2014

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