I have a collection of Objective-C classes that get sub-classed at a variety of different depths by a variety of different classes. Once the entire object is initialized (all of the sub-classes init functions have finished), I need to run an "Update Cache" method which then gets Overridden by the sub-classes as needed.
My Problem: With a variety of different inheritance depths to my class tree, there is no one place where I can put [self UpdateCache] and I can be sure that there is no sub-class that has not been initialized. The only possible solution would be to call [super init] after the initialization of each class so that the parent class is always called last. I want to avoid this as this goes against all guidelines of writing Objective-C. Is there any clean solution to this problem?
Here is an some example code:
@interface ClassA : NSObject
-(void)UpdateCache
@end
@interface ClassB : ClassA
-(void)UpdateCache
@end
@interface ClassC : ClassB
-(void)UpdateCache
@end
Now for the implementation we need to somehow get UpdateCahce to be called after we know all sub classes have initialized regardless of which class has been initialized
@implementation A
-(id)init
{
if(self = [super init])
{
// Placing [self UpdateCache] here would make it be called prior to
// B and C's complete init function from being called.
}
}
-(void)UpdateCache
{
}
@end
@implementation B
-(id)init
{
if(self = [super init])
{
// Placing [self UpdateCache] would result in UpdateChache not being
// called if you initialized an instance of Class A
}
}
-(void)UpdateCache
{
[super UpdateCache];
}
@end
@implementation C
-(id)init
{
if(self = [super init])
{
// Placing [self UpdateCache] would result in UpdateChache not
//being called if you initialized an instance of Class A or B
}
}
-(void)UpdateCache
{
[super UpdateCache];
}
@end