4

Having done a fair amount of google and looking on here I believe the following can't be done. I'm interesting in why it can't be done and people best workarounds, I offer mine below.

I use the simple department <---->> employee model, with both having a name attribute and the employee entity also having a timeWorked attribute.

The Problem

I would like to bind up a table with column one displaying departement.name and column two displaying department.@sum.employees.timeWorked. Attempting to do this results in the error:

[<_NSFaultingMutableSet 0x1d5e50> addObserver:forKeyPath:options:context:] is not supported. Key path: timeWorked
Cannot remove an observer <NSTableBinder 0x1a9280> for the key path "employees.timeWorked" from <Depatement 0x1faf40> because it is not registered as an observer.

However a bind to department.employees.@count does work (but I want more!).

Other Discussions

Here's another topic here with the same problem: nstablecolumn-binding-using-collection-operators-like-sum

And another: cocoa-bindings-binding-to-the-many-end-of-a-to-many-relationship

My Workaround

My solution to this problem is to create an attribute for the Department Entity which performs the sum:

-(NSNumber *)departmentHours {  
    return [self valueForKeyPath:@"employees.@sum.timeWorked"]; }

and to then simply bind to it.

It's only a little more code at add these attributes when I want to do some sort of collation / aggregation of data but I do feel it would be more elegant to be able to bind to department.@sum.employees.timeWorked.

Any Ideas?? Reasons why it doesn't work? Better workarounds?

Cheers All

Community
  • 1
  • 1
Quantum_Oli
  • 267
  • 2
  • 10
  • is it necessary to create an attribute or a category with the above method. – Gabe Rainbow Jun 13 '13 at 17:50
  • Doesn't have to be an attribute and saved in the persistent store. You can just have it be an @property on the department class. Changing any employee's timeWorked will not necessarily lead to a recalculation of departmentHours, though. Unless the employee's setTimeWorked changes a keyPath in keyPathsForValuesAffectingDepartmentHours – stevesliva Apr 05 '14 at 04:14

1 Answers1

0

You write about binding to department.@sum.employees.timeWorked - however, I think you should be binding to department.employees.@sum.timeWorked

Sam Hatchett
  • 513
  • 5
  • 11