Vladimir Prudnikov
Vladimir Prudnikov

Reputation: 7232

Loading and unloading of NSBundle

I'm building pluggable interface for my application. I stuck with an updates mechanism for the plugins.

Here is the code that confuses me.

- (void) unloadBundle{
    [_pluginInstance release], _pluginInstance = nil;
    [[self bundle] unload];
    [_bundle release], _bundle = nil;
}

- (void) loadBundleWithURL:(NSURL *)bundleURL{
    NSBundle *newBundle = [NSBundle bundleWithURL:bundleURL];
    if (newBundle){
        [self setBundle:newBundle];
        [self setPluginInstance:[[[[self.bundle principalClass] alloc] init] autorelease]];

        NSLog(@"New bundle: %@", self.bundle);
        NSLog(@"New bundle's principal class %@", [self.bundle principalClass]);
        NSLog(@"Principal class' bundle is %@", [NSBundle bundleForClass:[self.bundle principalClass]]);
        NSLog(@"Plugin's class %@", [self.pluginInstance class]);
    }
}

These are the methods of my wrapper around the plugin principal class. I just call unloadBundle and then loadBundleWithURL with the URL to the new version of the bundle. When executes it logs the following into console:

New bundle: NSBundle </Users/prudnikov/Work/Projects/***/Name.pluginextension> (loaded)
New bundle's principal class MyPluginClass
Principal class' bundle is NSBundle </Users/prudnikov/Library/Application Support/MyApp/PlugIns/Name.pluginextension> (not yet loaded)
Plugin's class MyPluginClass

Means that I take principal class from the new bundle, get its bundle with [NSBundle bundleForClass:] and it is old bundle.

Any ideas what I'm doing wrong?

Upvotes: 2

Views: 1436

Answers (1)

Vladimir Prudnikov
Vladimir Prudnikov

Reputation: 7232

The problem in this case was that I forgot to unload bundle in different place. I had method that was verifying that bundle is a valid plugin's bundle.

Calling principalClass loads the bundle automatically. So, calling unload is required.

Upvotes: 2

Related Questions