I have a NSManagedObject SharePrices, which contains DateTime attributes and NSNumbers for share prices and is stored in a standard Core Data “database”. As I want to group some of the share prices by month, I would like to create new SharePrice objects for each month and store in each of these objects the average share price for the month, the date would be set to the last day of the month.
How can I store this monthly information in my SharePrices NSManagedObject without the need to create it by using insertNewObjectForEntityForName and saving it to the store. Or would I need to create a category for the managed object and put there the grouping code? Basically, I just want to reuse my existing SharePrice structure without any Core Data support.
Thank you!
I would strongly recommend against re-using
NSManagedObject-derived objects outside of database context. The opportunity to reuse a few fields here and there does not worth the loss of clarity that will result from such an approach.The problem is, the aggregated share price object that you are planning to create does not represent a share price – instead, it represents a share price aggregation for a time period. Therefore it is not a good idea to use the same class to represent both kinds of objects: they are conceptually different. Another problem with your approach is that
NSManagedObjectincludes a lot of things that are of no use outside of database context, such as object ID, a reference to the managed object context, and so on. Keeping them around in a meaningless state would be misleading to anyone maintaining your application.