+ initialize the call more than once

I have a +initialize method that is called several times and I donโ€™t understand why.

According to the documentation, it will be called once for each class (and subclasses),

This is the code I'm using:

 @interface MyClass : NSObject @end static NSArray *myStaticArray; @implementation MyClass + (void)initialize { myStaticArray = [NSArray array]; } @end 

(obviously, there is another code, but the corresponding part).

No subclasses of MyClass . He does not do anything. Initialization + is called once when my application starts (the NSApplication delegate tells him to populate myStaticArray with data from disk). And then + initialize is called a second time when the user selects a menu item related to this class.

I just added dispatch_once() around my initialize code, and this obviously fixes my problem. But I do not understand what is happening? Why is this called more than once when there are no subclasses?

This is the stack trace on the first call + initialization:

 +[MyClass initialize] _class_initialize objc_msgSend -[MyAppDelegate applicationDidBecomeActive:] _CFXNotificationPost NSApplicationMain main start 

And here is the second call:

 +[MyClass initialize] _class_initialize NSApplicationMain main start 

As you can see, my code does not start the second call + initialize (nothing in the stack trace). This happens immediately after the window is displayed, in which the contents of the static array are cleared +initialize (the window displays the contents of the array, but immediately after that the array is empty).

+10
objective-c cocoa
Jan 01 '13 at 12:21
source share
3 answers

+initialize will be sent to each class on first call (by message), including dynamically created classes. At run time, there is no protection against starting execution several times. If the subclass is initialized but does not implement +initialize , no matter what is super up, the chain will call them again.

Orthogonally automatic KVO is implemented by creating dynamically derived subclasses of the class of the observed instance. This subclass +initialize d is exactly the same as any other class, thereby launching several executions of the parent class +initialize .

Runtime may take measures to protect against this. However, since +initialize always documented as potentially executable several times, which added complexity (this is surprisingly difficult, given that KVO classes come and go quite often, potentially) is not considered worthy of attention.

Current Recommended Template:

 + (void) initialize { static dispatch_once_t once; dispatch_once(&once, ^{ ... one time initialization here ... }); } 
+23
Jan 02 '13 at 16:46
source share

+initialize receives a call for each class in the inheritance chain, so if you initialize two classes that have the same superclass (or a superclass and one of its subclasses), then the superclass method +initialize will be called twice,

Could this be the reason?

+3
Jan 01 '13 at 12:30
source share

1.Runtime called + initialization method in superclass before subclass

2.if a subclass has no method, then it will call the initialization method of the parent class.

3. Use the always-chronically initialized method of the + (Void) initialization method {

 if(self==[Car Class]){ //initialize here your static var } } 

for a clear understanding read this post + (void) initialize

0
Feb 26 '15 at 12:48
source share



All Articles