I have a Core Data object (AGroup) with a to-many relationship to a collection of other Core Data objects (items, which are instances of ANItem). I'm using the AGroup object in a UITableView (this is on iOS) and the order in which the items are displayed is determined by a property on the items (sortProperty). So, code wise (ignoring that they are Core Data objects, so this is semi-pseudocode) it looks like this:
@interface AGroup : NSManagedObject
{
NSSet *items; // these are instances of ANItem
}
@property (retain) NSSet *items;
@end
@interface ANItem : NSManagedObject
{
NSNumber *sortProperty;
}
@property (retain) NSNumber *sortProperty;
@end
Ok, so, the point here is that the UITableView has an instance of an AGroup object and is displaying the list of items, and the order in which the items are displayed is determined by the sortProperty property.
So, I want to know when the sortProperty changes on an instance of ANItem so that I can update the table view. I'm wondering, is there a way I can use KVO to observe the sortProperty from the AGroup object? Something like:
[group addObserver:self forKeyPath:@"items.sortProperty" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil];
or even:
[group addObserver:self forKeyPath:@"items.@sum.sortProperty" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil];
would work, since the sum would change when the value changes.
I've tried this, though, and I get an invalid argument error.
Does anyone know if this is possible? If so, how?
I realize I could do a fetch request and sort the request by sortProperty, but due to some other application architecture limitations, that's not really possible in this case. So essentially, this kinda comes down to imagining that I'm just dealing with a plain old NSSet in an object kind of relationship. I only mention the core data aspect in case that impacts the answer due to additional limitations/etc?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…