views:

40

answers:

1

In data model I have transient property uppercaseFirstLetterOfName which will return first letter of persistent property. I specify this property in

NSSortDescriptor* sortByWordDescriptor = [[NSSortDescriptor alloc] 
                    initWithKey:@"subject" ascending:YES];
NSArray* sortArray = [[NSArray alloc]
                    initWithObjects:sortByWordDescriptor, nil];
[fetchRequest setSortDescriptors:sortArray];

NSFetchedResultsController* controller = [[NSFetchedResultsController alloc]
                    initWithFetchRequest:fetchRequest 
                    managedObjectContext:managedObjectContext 
                    sectionNameKeyPath:@"uppercaseFirstLetterOfName" 
                    cacheName:@"Root"];

When I change the persistent object taken from fetchedresultscontroller so the section should be deleted, only controller:didChangeObject fires. But since the section is gone(in fact) I would expect controller:didChangeSection also be fired. Should I do something extra when modifying persistent object in order to make controller:didChangeSection invoked?

UPD:

This is transient property getter in model subclass

- (NSString *)uppercaseFirstLetterOfName 
{
    [self willAccessValueForKey:@"uppercaseFirstLetterOfName"];
    NSString *aString = [[self valueForKey:@"subject"] uppercaseString];
    NSString *stringToReturn = [aString substringWithRange:
             [aString rangeOfComposedCharacterSequenceAtIndex:0]];
    [self didAccessValueForKey:@"uppercaseFirstLetterOfName"];

    return stringToReturn;
}

Here I get the object and pass to view controller to modify

...
detailViewController.unit = (ModelClass*)[fetchedResultsController 
                                    objectAtIndexPath:indexPath];
...

and finally data modification

unit.subject = someTextField.text;
...
[unit.managedObjectContext save:&error]

I don't have anything other special for transient object, so I don't modify it directly.

+1  A: 

WIthout seeing the code surrounding the update of that transient property I would have to guess that you are having a KVO issue. Can you post the code?

Update

Do you have a custom setter for subject? If not, do you have a dependent key set up for uppercaseFirstLetterOfName? If not, then that is your issue. Changing one value will not trigger the other value to change. This is a KVO situation.

NSFetchedResultsController is observing the uppercaseFirstLetterOfName specifically and waiting for it to change. It is not just watching the object to change. Therefore when you change the subject you need to "flag" uppercaseFirstLetterOfName as touched as well. The easiest way to do this is to add the following method to your NSManagedObject subclass:

+ (NSSet*)keyPathsForValuesAffectingUppercaseFirstLetterOfName
{
  return [NSSet setWithObject:@"Subject"];
}

This will tell KVO that whenever the subject property has changed that it should also fire a change notification for UppercaseFirstLetterOfName.

Marcus S. Zarra
@Marcus S. Zarra: updated
Michael
Answer updated.
Marcus S. Zarra
That may be the case, since I don't have neither one you mentioned. I'll give it a try. Thanks
Michael