Core Data to-many relationships and keyPathsForValuesAffectingValueForKey

  • Hi,

    I have set up a Core Data model with two entities, Account and Operation.
    Account has a to-many relationship With Operation named operations.
    Operation has an attribute amount and Account has a property total (not in the
    model) which is supposed to be the sum of the amount of all its operations.

    I would like to trigger a KVO notification for the key total of an account
    whenever the amount of one of its operations changes.

    I know in 10.4 it wasn't ever possible as the method
    setKeys:triggerChangeNotificationsForDependentKey: didn't work with keyPaths but
    I suppose I should be able to do it in 10.5 with
    keyPathsForValuesAffectingValueForKey.

    I implemented method keyPathsForValuesAffectingValueForKey in Account.m as
    follows :
    - (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
      NSMutableSet *keyPaths = [NSMutableSet setWithSet:[super
    keyPathsForValuesAffectingValueForKey:key]];
      if ([key isEqualToString:@"total"]) {
          [keyPaths addObject:@"operations.amount"];
      }
      return keyPaths;
    }

    When trying to run my program, I get an error saying that _NSFaultingMutableSet
    is not KVO compliant with key "amount".

    So, how am I supposed to do this ?

    Thanks,
    Cyril Anger
  • On 11/23/07 11:48 AM, <angeman7...> said:

    > I know in 10.4 it wasn't ever possible as the method
    > setKeys:triggerChangeNotificationsForDependentKey: didn't work with
    > keyPaths but
    > I suppose I should be able to do it in 10.5 with
    > keyPathsForValuesAffectingValueForKey.
    >
    > I implemented method keyPathsForValuesAffectingValueForKey in Account.m as
    > follows :
    > - (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
    > NSMutableSet *keyPaths = [NSMutableSet setWithSet:[super
    > keyPathsForValuesAffectingValueForKey:key]];
    > if ([key isEqualToString:@"total"]) {
    > [keyPaths addObject:@"operations.amount"];
    > }
    > return keyPaths;
    > }
    >
    > When trying to run my program, I get an error saying that
    > _NSFaultingMutableSet
    > is not KVO compliant with key "amount".

    I have also switched my code to use this new
    keyPathsForValuesAffectingValueForKey method.  I have not been able to
    make it work with key paths either.  I don't get the error you do, since
    I'm not using Core Data.  It just silently fails.  My code is slightly
    different (I don't always create a new set), but I doubt that accounts
    for it.  Anyway, here's how I do it:

    + (NSSet*)keyPathsForValuesAffectingValueForKey:(NSString*)key
    {
    NSSet* set = [super keyPathsForValuesAffectingValueForKey:key];
    if ([key isEqualToString:@"overallStatus"]) {
      set = [set setByAddingObjectsFromSet:[NSSet setWithObjects:
      @"subStatus1", @"subStatus2",
      @"someController.subStatus3", (id)nil]];
    }
    return set;
    }

    It works for subStatus1 and 2, but not 3.

    Anyone have working keyPathsForValuesAffectingValueForKey sample code?

    --
    ____________________________________________________________
    Sean McBride, B. Eng                <sean...>
    Rogue Research                        www.rogue-research.com
    Mac Software Developer              Montréal, Québec, Canada
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today