Re: How to observe Core Data add/remove actions?
Re: How to observe Core Data add/remove actions?
- Subject: Re: How to observe Core Data add/remove actions?
- From: Joshua Emmons <email@hidden>
- Date: Thu, 15 Nov 2007 12:14:24 -0600
Arthur,
Now I'm still puzzled on how to do the observations correctly.
Assuming you have an NSArrayController (let's call it recipeArrCont)
set for you Recipe entity and another NSArrayController whose content
set is bound to recipeArrCont.selected.items (we'll call it
itemArrCont), it seems as though you'd want to call:
[itemArrCont addObserver:self forKeyPath:@"arrangedObjects" options:0
context:"Something"];
Somewhere in your object's initialization routines. You would then get
messages sent to:
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:
(id)object change:(NSDictionary *)change context:(void *)context
{
if (context == "Something") {
//The array of Item entities changed for some reason.
}
else {
[super observeValueForKeyPath:keyPath ofObject:object
change:change context:context];
}
}
Because you're bound though the recipeArrCont's selected keypath, this
message will be sent any time the selected recipe changes as well as
whenever the itemArrCont's arrangedObjects change (due to additions/
subtractions/sorts/etc).
Don't forget to unregister the observer!
- (void) dealloc {
[itemArrCont removeObserver:self forKeyPath:@"arrangedObjects"];
[super dealloc];
}
Especially, on what to do from - observeValueForKeyPath.
Well... that depends largely on what you want to have happen. You've
told us that this is an array of Item entities that have properties
the object needs to know about, but that's it. If the goal is simply
to be able to read the properties of each Item entity, you could
retrieve those with something like:
for(NSManagedObject *obj in [arrayController
valueForKeyPath:@"arrangedObjects"]){
NSLog(@"property1: %@", [obj valueForKey:@"property1"]);
NSLog(@"property2: %@", [obj valueForKey:@"property2"]);
NSLog(@"property3: %@", [obj valueForKey:@"property3"]);
}
Does that help at all? If not, details details details! ;-)
Cheers,
-Joshua Emmons
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden