开发者

NSFetchedResultsController and transient property for sections

开发者 https://www.devze.com 2023-01-11 03:52 出处:网络
In data model I have transient property uppercaseFirstLetterOfName which will return first letter of persistent property. I specify this property in

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]
                    initWithFetchR开发者_JS百科equest: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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消