Re: Double Initialize - is that how it should be?
Re: Double Initialize - is that how it should be?
- Subject: Re: Double Initialize - is that how it should be?
- From: Chris Suter <email@hidden>
- Date: Tue, 17 Mar 2009 13:41:51 +1100
Hi Steve,
On Tue, Mar 17, 2009 at 1:21 PM, Steve Cronin <email@hidden> wrote:
> Folks;
>
> I use an object in my application called 'appDelegate'; it's a subclass of
> NSObject.
>
> I instantiate this object in the main Nib file
> This object has IBActions for the main menu and since it is -app delegate
> it is a kind on central dispatcher.
>
> My question is this:
> The +initialize is run twice when the app loads:
> 2009-03-16 20:21:42.690 XYZ[6676:813] *[AppDelegate initialize]
> 2009-03-16 20:21:42.781 XYZ[6676:813] *[NSKVONotifying_AppDelegate
> initialize]
>
> Once for the object itself and then again as the instance in the Nib.
> That's how I read this. Is that wrong?
>
> So I wonder if this is wasteful. I don't really want to run initialize
> twice.
> It kind of torques my notion of a class that +initialize is run twice but I
> try and just play along....
> I don't see how to use this IBActions and IBOutlets without instantiating.
> Is the correct thing to do here break this object into smaller objects?
I think the reason you're seeing this behaviour is because of the
isa-swizzling that happens for automated KVO notifications.
What's happening is that a new class is created with overridden
methods to perform automated KVO notifications. You're getting an
initialize message sent to this new class as well as the original.
There are a number of things you can do:
1. Don't use initialize and put the code somewhere else.
2. Use a static variable to record whether you've already run your
code or you could just test that the class is the original one.
3. Turn off automatic KVO notifications and do them manually.
Regards,
Chris
_______________________________________________
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