views:

63

answers:

1

I'm fairly new to Core Data, have looked through many tutorials and forums and haven't found the elegant solution to my problem:

I've got three entities in my model: Worker, Task, and SubTask. Each has a to-many relationship to the entity below it and a to-one inverse. Worker has many Tasks, each Task has many SubTasks, each SubTask has one parentTask, and each Task has one Worker. SubTask has a startTime attribute that gets set to the current time in the newObject method and a button that triggers endSubTask method in my SubTaskArrayController class. endSubTask calculates the time between startTime and now and sets that value for the SubTask attribute timeWorked. So far so good. The problem:

What is the best way to get the selected Task's totalTimeWorked attribute to update itself?

When I try something like

[currentSubTask setValue:newTotalTime forKey:@"parentTask.totalTimeWorked"];

I get:

[ setValue:forUndefinedKey:]: the entity SubTask is not key value coding-compliant for the key parentTask.totalTimeWorked.

How do I access the attributes of the parentTask of the currently selected SubTask?

Any help in pointing me in the right direction is appreciated.

+3  A: 

You are using a key path. You should use setValue:forKeyPath: instead.

Johan Kool
D'oh. I figured it was something basic I was missing. Thanks. Is this an OK practice or should I learn a solution (NSFetchRequests?) for Task to calculate it's totalTimeWorked itself?
jslanger
You can use `@sum`, `@count` and friends in key paths to do such calculations. I would simply store the duration in each subtask, and then sum those, instead of trying to store them in the task directly. For simple things like this you generally don't need `NSFetchRequest` yet.
Johan Kool
'[email protected]' works perfectly. Thanks.
jslanger