Implementing binding with garbage collection.
Implementing binding with garbage collection.
- Subject: Implementing binding with garbage collection.
- From: Joshua Emmons <email@hidden>
- Date: Thu, 8 Nov 2007 14:52:58 -0600
Thanks, everyone, for your private replies, but I'm getting some
contradictory information so I'm going to try to duke it out in the
public forum again ;-)
The basic premise is that I have an NSView subclass in a GC-Required
app which has an NSArray* property called contentArray. I would like
to bind contentArray to the arrangedObjects property of an
NSArrayController. In this case, my NSView subclass conveniently
contains an outlet to said controller. My code therefore looks
something like this:
@interface MyView : NSView {
IBOutlet NSArrayController *myArrayController;
}
@property(copy)NSArray *contentArray;
@property(copy) NSString *observedKeyPath;
@property id observedObject;
@end
@implementation MyView
@synthesize contentArray, observedObject, observedKeyPath;
-(void)awakeFromNib{
[self bind:@"contentArray" toObject:arrayController
withKeyPath:@"arrangedObjects" options:nil]
}
-(void)bind:(NSString *)binding toObject:(id)observableController
withKeyPath:(NSString *)keyPath options:(NSDictionary *)options{
if([binding isEqualToString:@"entities"]){
self.observedObject = observableController;
self.observedKeyPath = keyPath;
[observableController addObserver:self forKeyPath:keyPath
options:0 context:someContext];
}else{
[super bind:binding toObject:observableController
withKeyPath:keyPath options:options];
}
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context{
//update contentArray when the obeservedObject changes...
}
-(void)setContentArray:(NSArray*)anArray{
//communicate changes back to observedObject...
}
-(void)finally{
[self.observedObject removeObserver:self
forKeyPath:self.observedKeyPath];
self.observedObject = nil;
self.observedKey = nil;
}
@end
My concern is with the -finally. I basically just moved my -dealloc
code over there, but in doing so I'm explicitly performing two of the
things the documentation says never to perform in -finally:
"To make your finalize method as efficient as possible, you should
typically not do any of the following:
o Set instance variables to nil
o Remove self as an observer of a notification center (in a garbage
collected environment, notification centers use zeroing weak
references)."
Now to be fair, it's not a notification center that I'm observing, but
it's been suggested the same applies? And while I could get away with
not nilling out the keypath, I'm unclear what will happen if I don't
clear my reference to the array controller. On top of all of that,
it's not clear to me in my testing that -finally is reliably called to
begin with.
All of this combines to make coding a binding implementation even more
convoluted than it was in 10.4. I feel certain that can't be the case
and that I am again overlooking something. Does anyone have experience
to share?
Many thanks,
-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