Search code examples
iosobjective-ccore-datansmanagedobjectnsmanagedobjectcontext

Understanding of NSCoreData and MSManagedObject subclasses


I am learning a bit on NSCoreData and before introducing it some existing projects I have, I would like to validate my good understanding of the core principles. From what I have understood, NSCoreData make it easier to manage local storage of object (+retrieval after that) by subclassing our Model class from NSManagedObject rather than from NSObject.

Right ?

I have a few questions then. Let's consider I am building a real estate application with as core model object the class Property that can represent an appartment, a house, and all related information. Currently it is managed in my app as a subclass of NSObject.

1) I retrieve the properties from the server through a search query, and have written a initWithJson : method to populate each instance. Now if I subclass Property from NSManagedObject, I will create my instances by using

+(id)insertNewObjectForEntityForName:(NSString *)entityName 
                   inManagedObjectContext:(NSManagedObjectContext *)context

and I will be still be able to add a populateWithJson: to my class to fill in the properties.

Then I will create a lot of Property instances in the current managedObjectContext, and if I do a save, they will be stored at the physical layer.

If I call again the same webservice, and retrieve the same JSON content, I will recreate the identical managed objects. How to avoid redundancy with the [managedObjectContext save:&error] call and not to store physically several time the representation of a single real life property ?

2) Let's say I want to store physically only some properties, for instance only the one the user want to have as favorites.

[managedObjectContext save:&error] will save all created / modified / deleted managed objects from the context to the physical layer, and not only the one I want.

How to achieve that ?

Am I supposed to declare another context (managedObjectContext2), move the instance I want to store in that context, and do the save in that one ?

(I mean, I will have a context just to manipulate the object, create instances from the JSON and represents them in UI ... and a second one to actually do the storage)

Or am I supposed to stores all the objects, and add a isFavorite BOOL property , and then fetching using a predicate on that property ?

3) The app has a common navigation pattern : the UITableView lists Properties instance with the minimum information required, and going on a detail view call a webservice to request more information on a specific Property instance (images, full text description).

Is it a good practice for instance to call the webservice only if the property.fullDescription is nil, and then update the object and store it locally with all detailed information, and the next time only to fetch it locally with a predicate on the property.id ? What about object that might be updated server-side after they have been created?

Thanks for your lights


Solution

  • 1) Retrieve the server data into a temporary form (array of dictionaries?), then for each possible property in the array, check to see if you already have an object in Core Data that matches. If you do, either ignore it or update any changed attributes; if not, create a Property object.

    2) Decide which things you want to persist in order to support your app's functions. There's no point in creating a managed object for something you don't want to save. Note, though, that Core Data supports sub-classes if you want both Property and FavoriteProperty.

    3) Entirely up to your "business rules"…. How often do you need local data to be updated? The only technical consideration might be the guideline to not keep large files locally that can be re-created on demand.