mirror of
https://github.com/sparkle-project/Sparkle
synced 2026-11-24 04:38:16 +00:00
Improve -[SUHost objectForInfoDictionaryKey:]
We use the cached host bundle if the host bundle is equal to the main bundle. Otherwise we read in the latest Info dictionary every time. The cached case is the most common case. The odd case (updating other bundles) is less common. Thus we have better performance for the common case, and we retain correct code in both cases.
This commit is contained in:
1 parent
3b2ea83c6f
commit
b645005689
3 files changed
+18
-20
No files matched your search
Vendored
-4
@@ -526,10 +526,6 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification
|
||||
[self scheduleNextUpdateCheck];
|
||||
return;
|
||||
}
|
||||
|
||||
// Reload our host bundle information in case its current cache is out of date
|
||||
// We could have updated the bundle before for example without terminating this updater process
|
||||
[self.host reloadInfoDictionary];
|
||||
|
||||
// Because an application can change the configuration (eg: the feed url) at any point, we should always check if it's valid
|
||||
NSError *configurationError = nil;
|
||||
|
||||
Vendored
-4
@@ -13,10 +13,6 @@
|
||||
|
||||
- (instancetype)initWithBundle:(NSBundle *)aBundle;
|
||||
|
||||
// Invalidates the info dictionary cache
|
||||
// This is not thread safe
|
||||
- (void)reloadInfoDictionary;
|
||||
|
||||
@property (readonly, copy) NSString *bundlePath;
|
||||
@property (readonly, copy) NSString *name;
|
||||
@property (readonly, copy) NSString *version;
|
||||
|
||||
Vendored
+18
-12
@@ -24,18 +24,18 @@
|
||||
@interface SUHost ()
|
||||
|
||||
@property (strong, readwrite) NSBundle *bundle;
|
||||
@property (nonatomic, readonly) BOOL isMainBundle;
|
||||
@property (copy) NSString *defaultsDomain;
|
||||
@property (assign) BOOL usesStandardUserDefaults;
|
||||
@property (nonatomic) NSDictionary *infoDictionary;
|
||||
|
||||
@end
|
||||
|
||||
@implementation SUHost
|
||||
|
||||
@synthesize bundle;
|
||||
@synthesize isMainBundle = _isMainBundle;
|
||||
@synthesize defaultsDomain;
|
||||
@synthesize usesStandardUserDefaults;
|
||||
@synthesize infoDictionary = _infoDictionary;
|
||||
|
||||
- (instancetype)initWithBundle:(NSBundle *)aBundle
|
||||
{
|
||||
@@ -47,7 +47,7 @@
|
||||
SULog(SULogLevelError, @"Error: the bundle being updated at %@ has no %@! This will cause preference read/write to not work properly.", self.bundle, kCFBundleIdentifierKey);
|
||||
}
|
||||
|
||||
_infoDictionary = aBundle.infoDictionary;
|
||||
_isMainBundle = [aBundle isEqualTo:[NSBundle mainBundle]];
|
||||
|
||||
self.defaultsDomain = [self objectForInfoDictionaryKey:SUDefaultsDomainKey];
|
||||
if (!self.defaultsDomain) {
|
||||
@@ -61,14 +61,6 @@
|
||||
return self;
|
||||
}
|
||||
|
||||
// NSBundles always cache the info dictionary, even if you create a new NSBundle instance, but we sometimes want to reload it
|
||||
// in case the bundle changes or is updated
|
||||
- (void)reloadInfoDictionary
|
||||
{
|
||||
CFDictionaryRef infoDictionary = CFBundleCopyInfoDictionaryInDirectory((CFURLRef)self.bundle.bundleURL);
|
||||
self.infoDictionary = CFBridgingRelease(infoDictionary);
|
||||
}
|
||||
|
||||
- (NSString *)description { return [NSString stringWithFormat:@"%@ <%@>", [self class], [self bundlePath]]; }
|
||||
|
||||
- (NSString *)bundlePath
|
||||
@@ -163,7 +155,21 @@
|
||||
|
||||
- (id)objectForInfoDictionaryKey:(NSString *)key
|
||||
{
|
||||
return [self.infoDictionary objectForKey:key];
|
||||
if (self.isMainBundle) {
|
||||
// Common fast path - if we're updating the main bundle, that means our updater and host bundle's lifetime is the same
|
||||
// If the bundle happens to be updated or change, that means our updater process needs to be terminated first to do it safely
|
||||
// Thus we can rely on the cached Info dictionary
|
||||
return [self.bundle objectForInfoDictionaryKey:key];
|
||||
} else {
|
||||
// Slow path - if we're updating another bundle, we should read in the most up to date Info dictionary because
|
||||
// the bundle can be replaced externally or even by us.
|
||||
// This is the easiest way to read the Info dictionary values *correctly* despite some performance loss.
|
||||
// A mutable method to reload the Info dictionary at certain points and have it cached at other points is challenging to do correctly.
|
||||
CFDictionaryRef cfInfoDictionary = CFBundleCopyInfoDictionaryInDirectory((CFURLRef)self.bundle.bundleURL);
|
||||
NSDictionary *infoDictionary = CFBridgingRelease(cfInfoDictionary);
|
||||
|
||||
return [infoDictionary objectForKey:key];
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)boolForInfoDictionaryKey:(NSString *)key
|
||||
|
||||
Reference in new issue
Block a user