Good design using CoreData?

To use CoreData, many developers inherit model objects from NSManagedObject .

But what if I want my model objects to be independent of the storage mechanism used (maybe I will reuse them in a project where I don't need persistence)?

My solution so far has been to create for each class of the model a class that inherits from NSManagedObject , which has a reference to the model object and loads and saves it.

What is the best approach?

+4
source share
3 answers

The best way to work with Core Data is to inherit model objects from NSManagedObject. You can then control whether to persist or not out of context, as TechZen pointed out.

If you still insist on not inheriting model classes from NSManagedObject, this can help combine protocols and categories, as described by RyanR. However, you will have, in my opinion, a large invoice that creates protocols, categories, and duplicate classes.

+1
source

There is currently no way to create objects that can be stored in Core Data, and also completely disconnected from Core Data. Everything that inherits from NSManagedObject carries some baggage, not least because you cannot serialize it without any special code for each object.

You can create several protocols representing each of your entities, which correspond to your managed objects with master data, as well as your stand-alone objects. You can use categories in NSObjects that match only your entity protocol to implement the behavior shared by master data and autonomous objects. Applying a category only to classes that match your entity protocol, you forbid applying a category to every NSObject (very bad). Instead, it only applies to classes that you implement that match this category. This is the only way to find common code between Core Data objects and stand-alone objects.

To prevent any changes made to your Core Data objects every time you change your model, I use mogenerator to restore objects instead of Xcode. This allows me to separate the data related to the main data from any settings that I need on the objects (for example, applying the protocol for the object). If you are using Xcode 3, mogenerator includes a plugin that handles Xcode integration. If you are on Xcode 4, the plugin does not work, but with the help of another DevForums developer, I wrote a tutorial on getting a mogenerator to work.

+3
source

But what if I want to save my model objects that are not dependent on the storage mechanism that is used (maybe I will reuse them in a project where I dont need persistency at all)?

With master data, persistence is just an option, it is not required because Core Data is not primarily a persistence API. Instead, it is an object graph management API designed to provide a complete layer of the Design-View-Controller application model. This true function controls the objects in the graph to accurately model / simulate real-world objects, conditions or events and the relationships between them.

You change the save settings at the permanent repository coordinator level. You have the option of using sqlite repository, binary store, xml plist repository, or in-memory repository, which, as the name implies, is not really a "persistent" repository. You also have the opportunity to write your own store. For more information, see Atomic Store programming topics .

You really need a managed entity context to get any value from Core Data. Instead of calling it "NSManagedObjectContext", they should have called it "NSManagedObjectManager" because it is all automatic maintenance of the object schedule. If you want to duplicate this functionality, you will have to basically write your own manager class.

In my opinion, the best way to provide insurance flexibility that you are trying to write in a serialization method to a common data format such as JSON. Then, if you need to switch to another save option, you can simply convert the graph of the kernel data object to JSON and send it anywhere.

However, I really would not bother if you already do not know for sure that such functionality is needed. It is very, very rare. It is better to code only for known requirements, and then just record the time for new requirements when they really appear.

+1
source

All Articles