0

I have the following datamodel to manage measurements (heart rate and skin response) enter image description here

When I'm acquiring a new measurement it's going to be an entity of "MinuteStress" Now I want to programmatically check if a corresponding day and month entity exist and if not create one automatically and add my measurement to their average.

My first question would be: Where is the right place to check for the super entities? Is it a good idea to do this in the NSManagedObjectSubclass of "MinuteStress" or is it better to do so after I create the entity in my viewcontroller?

My second question would be if there is a smart way to create super entities from a sub entity?

Fabian
  • 4,160
  • 20
  • 32
DarkCell
  • 170
  • 1
  • 11

1 Answers1

0

In theory you can do that in the awakeFromInsert method of your NSManagedObject subclass, but that's a Bad Idea (tm) because you can trigger other Core Data events... see the "special considerations" section under awakeFromInsert in the Apple Docs for more info.

You'd be better off to query for the superclasses in the view controller and create them if needed, then create the MinuteStress instance.

You might also want to write some convenience methods for creating related child objects (like -(DayStress *) createDayStress] on MonthStress for example) where you create a child object and automatically set its parent reference (and any initialization values) before returning it. It makes the code flow in the view controller much nicer IMO.

Kevin Clifton
  • 509
  • 5
  • 5
  • Don't place the measurement logic into the viewController. Keep the viewController focused on controlling the view. Create a new class and do the averaging there. – Olaf Mar 25 '14 at 12:57