• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How dangerous is +load?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How dangerous is +load?


  • Subject: Re: How dangerous is +load?
  • From: Marco Scheurer <email@hidden>
  • Date: Thu, 1 May 2003 16:22:47 +0200

On Wednesday, April 30, 2003, at 08:34 PM, Georg Tuparev wrote:

Folks,

Here a method that works:

+(void)load
{
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(configureTestingUI:)
name: NSApplicationDidFinishLaunchingNotification
object: nil];
}

... but is dangerous because there is no guarantee that at the time the Notification Center is loaded. But I really do not want to create any dependancies between the class in question and NSApp or its delegate. I have done some digging, and it seams Foundation classes are always loaded before the classes from my framework, so in all examples the method above did not caused any trouble. But I would prefer to replace it. (For good or bad, +initialize is not called if no other method is sent to the class).

Any suggestions how can I achieve the same functionality in a more kosher way?


A pattern I've seen and used implements +load like this:

+ (void) load
{
[self class];
}

This will make sure that +initialize is called very early, so that you can do what's needed there. However, I'm not sure it's any safer: initialize will be sent right away, and there may still no guarantees that NSNotificationCenter is loaded. This could be checked with objc_lookUpClass from objc-runtime.h, but what could be done, at this level, except logging or quitting?.

(It's not better to use performSelector:withObject:afterDelay: in +load because an NSAutorelease pool is needed, so the NSAutorelease class needs to be here.)

Sending +class to force +initialize to be sent to classes loaded from a plug-in could also be done from your application, on NSBundleDidLoadNotification. Another alternative could be OmniBase's +didLoad, but I've never used it.

In the end, I would count on your +load method to work, especially in plug-ins, where I think it is safe to assume that all Foundation classes have been loaded. In fact, +load may sometimes be the only solution for bundles.

Marco Scheurer
Sen:te, Lausanne, Switzerland http://www.sente.ch
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: WeakCache?
  • Next by Date: Dictionary Enumerator - partial/wildcard seaches?
  • Previous by thread: Re: How dangerous is +load?
  • Next by thread: RE: getting an EventRecord?
  • Index(es):
    • Date
    • Thread