Do people currently turn to managed objects that extend NSManagedObject as domain objects, or are you creating separate classes strictly for storage and creating helper methods to create them in domain objects? I sometimes find all the conservation logic from the domain should be good.
If you create completely independent domain objects, you have the cost of synchronizing them with the Core Data model and saving the translation between the main data and these objects - plus you have duplicate objects in memory, depending on how many objects you have may be a problem.
However, the advantage of using separate domain objects is that you are no longer tied to the context of the managed object. One case where something like this can hurt you is if you maintain references to managed objects, and then some background operation causes the main object of the managed object to delete objects - now if you get access to any property in a remote managed entity, you throw a failure exception (even if you explicitly loaded the entity without errors).
One thing I have tried with moderate success is random, very lightweight individual data objects for specific purposes - I did this to define a protocol for representing access objects to data objects with the same names as the main data access providers. Then I had both the main data object, and the user stand-alone data object implemented this protocol and had a mechanism for automatically copying properties from one to another. Therefore, I did not make each object as user-defined and could process objects that either come from local storage or are autonomously interchangeable.
I have no obvious preference for this, but I try to use managed objects directly because of the lack of duplication. You can mitigate the negative side effects by listening to the changes or using the main data controller class.
One thing that helps keep domain objects and data objects the same, but not yet, uses mogenerator to create data objects. It generates very good object representations of objects in your master data warehouse, as well as the interface objects you want to edit — adding custom accessories or complex methods. When changing the data store, mogenerator restores the data object, but leaves its own code alone.
http://rentzsch.github.com/mogenerator/
How about cleaning? How does one usually delete all data when the application closes or perhaps the local storage expires? Of course, I do not want to save data about phone users at any time.
The data is usually small enough and I just leave it where the expiration timestamp is used so that you know when the data is too old to use directly. There is considerable value for storing data, because users close and reopen applications so often, and existing data allows you to instantly display results when showing content updates.
Is there any type of atomicity with basic data? My implementation will first check the data locally before hitting the web services, I would like to make sure that there is never half a data set intended for local storage and get funny results.
The atomicity is that you perform operations in a context, and then specify the context to save. Thus, true atomicity means abandonment of other components that issue preservation before you are ready, which usually means doing something in your own context and combining it in the main context.
I would like to run several background threads to retrieve data in the background, are there any things that I should consider when saving objects to the background thread?
Each background thread needs its own context, you must listen to the notification of conservation and merge into the main context at that time.
You should strive to avoid duplication of queries that can be stored on the same objects almost simultaneously, this can sometimes lead to kernel data errors during merging. In this regard, install the merge policy in the main context, since the default policy is an exception.
It also means that when modeling, you can use as many separate objects as possible, rather than one large object that aggregates data from many different sources.
For more information about saving and combining in other contexts, see this question:
CoreData and mergeChangesFromContextDidSaveNotification
In connection with the above question, what is the best way to create a “background fetch”? In the delegate application? To view depending on View? etc...?
I like to do this from a separate singleton class (after all, AppDelegate itself is a singleton ...) that I can request the context of the main managed entity in addition to the context specific to the stream.
It’s also useful in that when you start a new Core Data project, you don’t need to use the Core Data template and just repeat the use of this basic data manager.