Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
807 views
in Technique[技术] by (71.8m points)

ios - Using KVO to observe changes to a property on an object inside a collection in Objective-C

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:@"[email protected]" 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Im pretty sure there's no way to directly observe the contents of a collection like that. I think the best bet is to define the KVC/KVO compliant set accessors/mutators, observe self.items, and use the change notifications to directly observe sortProperty on each of the objects as they are added to the collection.

Since this is Core Data, you may also/instead need to use the context save notifications to detect changes in items. I don't have enough experience with Core Data to say for sure though.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...