Core Data Logic Distribution?

I used Core Data for a while, but I just asked myself a question: I always always create some kind of local storage class from which I control the Core Data model, it will be a singleton class that has a link to the context of the managed object, I have methods for creating new managed objects, deleting objects, saving, etc .... And my subclasses of Managed Object are really just models.

But I often worked with projects of other nations, and sometimes other developers tend to add more logic to subclasses of managed objects in the form of class methods and have a very simple or sometimes missing class for wrapping kernel data for everything.

For example, I would usually do something like this:

User *me = [[MyDataStore getInstance] createUserWithName:@"Daniel"]; 

While others are more likely to have:

 User *me = [User userWithName:@"Daniel"]; 

Obviously, the latter is much nicer and, in my opinion, more human-friendly, but in the end there may be a lot of fragmented code, but, on the other hand, my decision means that you have a very large file that is small, passes by a certain lengths.

I wondered if others could share their point of view on this. Thanks.

+4
source share
1 answer

From the logical code for placing the MVC point in a subclass of NSManagedObject, this is a more correct thing. Personally, I think this is more logical, i.e. This code creates a new user instance, so I put it in the User class.

At a practical level, I hated placing logical code in subclasses of NSManagedObject, because if I modified my model everything would disappear, however now I use MoGenerator , which takes care of all this, and I don’t have to worry about my user logic redefined.

Another thing I'm trying to use these days with CoreData is abstract entities. Not only are they great for simplifying your model, but also for your custom logic, I mean these are just subclasses wrapped around CoreData.

Just my opinion and what works for me at the moment, I hope this helps someone, we look forward to any other contributions!

+4
source

All Articles