Observing managed arrays in custom views?
Observing managed arrays in custom views?
- Subject: Observing managed arrays in custom views?
- From: Jonathan del Strother <email@hidden>
- Date: Tue, 7 Mar 2006 23:52:51 +0000
I'm a little confused over Core Data & array controller bindings /
observings, and was hoping to run through what I've got so far to see
if it's actually the 'right' way of doing things.
Sorry for the over-long email.
I have a Core Data entity 'MovieClip' bound to an NSArrayController's
managedObjectContext binding. So far, so good.
I have a subclassed NSView which is intended to display a
representation of all the clips in the array. So I need to do -
exposeBinding:@"clips" - so that the clips binding appears in the
Interface Builder inspector. This then requires the custom view to
implement 'setClips' and 'clips' methods (despite the fact that I'm
not really interested in retaining the _NSControllerArrayProxy that
gets passed in)
The view's clips parameter is then bound to the array controller's
arrangedObjects.
I then override the custom view's bind:toObject:... in order to be
able to observe changes in the managed array :
- (void)bind:(NSString *)binding toObject:(id)observableController
withKeyPath:(NSString *)keyPath options:(NSDictionary *)options
{
[super bind:binding toObject:observableController
withKeyPath:keyPath options:options];
[observableController addObserver:self forKeyPath:keyPath
options:NSKeyValueObservingOptionNew context:ClipsArrayIdentifier];
}
When an element is added/removed from the array, I want to update the
view. But this doesn't tell us when the contents of an element
changes, so we also need to observe each element in the array.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)
object change:(NSDictionary *)change context:(void *)context
{
[self setNeedsDisplay:YES];
if (context == ClipsArrayIdentifier) //If the contents of the array
is changed
{
NSArray* array = [object arrangedObjects];
[array addObserver:self toObjectsAtIndexes:
[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,[array count])]
forKeyPath:@"title"
options:NSKeyValueObservingOptionNew
context:ClipsObjectIdentifier];
}
}
...which has a couple of problems. First, the change dictionary
doesn't seem to contain the newly added/removed elements, so we don't
know what the new elements are, so we end up (re-)adding the observer
to every element in the array.
Secondly, there are a number of Clip attributes that I'm interested
in - not just the title. Is there a way of observing all attributes
without specifying each one manually?
I feel like I'm using a very long way round here. Is there a better
way of accomplishing the above?
If you actually read all that, thanks a lot for your time
Jon
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden