Re: [super initialize]?
Re: [super initialize]?
- Subject: Re: [super initialize]?
- From: Jim Correia <email@hidden>
- Date: Fri, 2 Sep 2005 22:20:01 -0400
On Sep 2, 2005, at 7:36 PM, mmalcolm crawford wrote:
If, however, you add a dependent key, 'weeklyCost', to the Employee
class, and implement its initializer:
+ (void)initialize {
[self setKeys:[NSArray arrayWithObjects:@"hourlyWage",
@"hoursPerWeek", nil]
triggerChangeNotificationsForDependentKey:@"weeklyCost"];
}
then KVO notifications are not sent for 'fullName' for instances of
Employee.
What is your recommended solution to this problem? This feels like a
common enough thing to want to do that the framework should provide a
mechanism (which is why I filed an enhancement request.)
Absent such mechanism my approach was to have all my managed objects
inherit from MyManagedObject which does something like this:
@implementation MyManagedObject
- (id)initWithEntity:(NSEntityDescription *)entity
insertIntoManagedObjectContext:(NSManagedObjectContext *)context
{
// lazy register dependent keys - see header documentation for
why it is done this way
[[self class] registerDependentKeysIfNecessary];
return [super initWithEntity: entity
insertIntoManagedObjectContext: context];
}
+ (void)registerDependentKeys
{
// no dependent keys to register in the base class, subclasses
must message super
}
+ (void)registerDependentKeysIfNecessary
{
static NSMutableDictionary *processedClasses = nil;
if (processedClasses == nil)
processedClasses = [[NSMutableDictionary alloc] init];
NSString *classname = NSStringFromClass([self class]);
if (nil == [processedClasses objectForKey: classname])
{
[self registerDependentKeys];
[processedClasses setObject: classname forKey: classname];
}
}
@end
- Jim
_______________________________________________
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