From 01899e8812009f819cb610f8083c563680435c11 Mon Sep 17 00:00:00 2001 From: echo Date: Thu, 4 Apr 2024 10:14:23 -0700 Subject: [PATCH 1/7] update singleton --- Sources/BranchSDK/BranchContentDiscoverer.m | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Sources/BranchSDK/BranchContentDiscoverer.m b/Sources/BranchSDK/BranchContentDiscoverer.m index c9fa3394e..6bf6ff24c 100644 --- a/Sources/BranchSDK/BranchContentDiscoverer.m +++ b/Sources/BranchSDK/BranchContentDiscoverer.m @@ -26,13 +26,12 @@ @interface BranchContentDiscoverer () @implementation BranchContentDiscoverer + (BranchContentDiscoverer *)getInstance { - static BranchContentDiscoverer *sharedInstance = nil; - @synchronized (self) { - if (!sharedInstance) { - sharedInstance = [[BranchContentDiscoverer alloc] init]; - } + static BranchContentDiscoverer *sharedInstance; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedInstance = [BranchContentDiscoverer new]; + }); return sharedInstance; - } } - (void) dealloc { From 5fe7f289a5b94cbeb0ef11bdbb81ecd3d8ada21e Mon Sep 17 00:00:00 2001 From: echo Date: Thu, 4 Apr 2024 10:16:15 -0700 Subject: [PATCH 2/7] update singleton --- Sources/BranchSDK/BranchContentDiscoveryManifest.m | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Sources/BranchSDK/BranchContentDiscoveryManifest.m b/Sources/BranchSDK/BranchContentDiscoveryManifest.m index e10585954..0354b2174 100644 --- a/Sources/BranchSDK/BranchContentDiscoveryManifest.m +++ b/Sources/BranchSDK/BranchContentDiscoveryManifest.m @@ -34,13 +34,12 @@ - (instancetype)init { } + (BranchContentDiscoveryManifest *)getInstance { - @synchronized (self) { - static BranchContentDiscoveryManifest *contentDiscoveryManifest = nil; - if (!contentDiscoveryManifest) { - contentDiscoveryManifest = [[BranchContentDiscoveryManifest alloc] init]; - } - return contentDiscoveryManifest; - } + static BranchContentDiscoveryManifest *sharedInstance; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedInstance = [BranchContentDiscoveryManifest new]; + }); + return sharedInstance; } - (void)onBranchInitialised:(NSDictionary *)branchInitDict withUrl:(NSString *)referringURL { From c93d8824d658af26d61d23b5e4bcab42b42dc367 Mon Sep 17 00:00:00 2001 From: echo Date: Thu, 4 Apr 2024 10:30:07 -0700 Subject: [PATCH 3/7] Add in default values to singletons, just to be explicit. Remove unnecessary synchronized from BranchJSONConfig --- .../Branch-SDK-Tests/BNCEncodingUtilsTests.m | 4 ++-- Sources/BranchSDK/BNCAppGroupsData.m | 4 ++-- Sources/BranchSDK/BNCAppleReceipt.m | 4 ++-- Sources/BranchSDK/BNCCallbackMap.m | 4 ++-- Sources/BranchSDK/BNCDeviceInfo.m | 2 +- Sources/BranchSDK/BNCEncodingUtils.m | 5 ++--- Sources/BranchSDK/BNCEventUtils.m | 4 ++-- Sources/BranchSDK/BNCPartnerParameters.m | 4 ++-- Sources/BranchSDK/BNCPasteboard.m | 4 ++-- Sources/BranchSDK/BNCPreferenceHelper.m | 4 ++-- Sources/BranchSDK/BNCQRCodeCache.m | 4 ++-- Sources/BranchSDK/BNCReachability.m | 4 ++-- Sources/BranchSDK/BNCSKAdNetwork.m | 4 ++-- Sources/BranchSDK/BNCServerAPI.m | 4 ++-- Sources/BranchSDK/BNCServerRequestQueue.m | 8 ++++---- Sources/BranchSDK/BNCUserAgentCollector.m | 4 ++-- Sources/BranchSDK/BranchContentDiscoverer.m | 4 ++-- .../BranchSDK/BranchContentDiscoveryManifest.m | 4 ++-- Sources/BranchSDK/BranchJsonConfig.m | 17 +++++++---------- Sources/BranchSDK/BranchLogger.m | 2 +- Sources/BranchSDK/BranchPluginSupport.m | 4 ++-- Sources/BranchSDK/BranchScene.m | 4 ++-- Sources/BranchSDK/NSError+Branch.m | 4 ++-- 23 files changed, 51 insertions(+), 55 deletions(-) diff --git a/Branch-TestBed/Branch-SDK-Tests/BNCEncodingUtilsTests.m b/Branch-TestBed/Branch-SDK-Tests/BNCEncodingUtilsTests.m index 54107d2b5..0b18fe51a 100644 --- a/Branch-TestBed/Branch-SDK-Tests/BNCEncodingUtilsTests.m +++ b/Branch-TestBed/Branch-SDK-Tests/BNCEncodingUtilsTests.m @@ -291,8 +291,8 @@ - (void)testDecodeQueryStringToDictionary { #pragma mark - Test Util methods - (NSString *)stringForDate:(NSDate *)date { - static NSDateFormatter *dateFormatter; - static dispatch_once_t onceToken; + static NSDateFormatter *dateFormatter = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ dateFormatter = [[NSDateFormatter alloc] init]; diff --git a/Sources/BranchSDK/BNCAppGroupsData.m b/Sources/BranchSDK/BNCAppGroupsData.m index 8ce782b95..cb4908f86 100644 --- a/Sources/BranchSDK/BNCAppGroupsData.m +++ b/Sources/BranchSDK/BNCAppGroupsData.m @@ -18,8 +18,8 @@ @interface BNCAppGroupsData() @implementation BNCAppGroupsData + (instancetype)shared { - static BNCAppGroupsData *appGroupsData; - static dispatch_once_t onceToken; + static BNCAppGroupsData *appGroupsData = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ appGroupsData = [BNCAppGroupsData new]; }); diff --git a/Sources/BranchSDK/BNCAppleReceipt.m b/Sources/BranchSDK/BNCAppleReceipt.m index ea54b4947..3536b7e89 100644 --- a/Sources/BranchSDK/BNCAppleReceipt.m +++ b/Sources/BranchSDK/BNCAppleReceipt.m @@ -23,8 +23,8 @@ @interface BNCAppleReceipt() @implementation BNCAppleReceipt + (BNCAppleReceipt *)sharedInstance { - static BNCAppleReceipt *singleton; - static dispatch_once_t onceToken; + static BNCAppleReceipt *singleton = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ singleton = [BNCAppleReceipt new]; }); diff --git a/Sources/BranchSDK/BNCCallbackMap.m b/Sources/BranchSDK/BNCCallbackMap.m index 8c1803824..ae5e4e829 100644 --- a/Sources/BranchSDK/BNCCallbackMap.m +++ b/Sources/BranchSDK/BNCCallbackMap.m @@ -15,8 +15,8 @@ @interface BNCCallbackMap() @implementation BNCCallbackMap + (instancetype)shared { - static BNCCallbackMap *map; - static dispatch_once_t onceToken; + static BNCCallbackMap *map = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ map = [BNCCallbackMap new]; }); diff --git a/Sources/BranchSDK/BNCDeviceInfo.m b/Sources/BranchSDK/BNCDeviceInfo.m index ef933b86d..dd6f1926d 100644 --- a/Sources/BranchSDK/BNCDeviceInfo.m +++ b/Sources/BranchSDK/BNCDeviceInfo.m @@ -34,7 +34,7 @@ @interface BNCDeviceInfo() @implementation BNCDeviceInfo + (BNCDeviceInfo *)getInstance { - static BNCDeviceInfo *bnc_deviceInfo = 0; + static BNCDeviceInfo *bnc_deviceInfo = nil; static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ bnc_deviceInfo = [BNCDeviceInfo new]; diff --git a/Sources/BranchSDK/BNCEncodingUtils.m b/Sources/BranchSDK/BNCEncodingUtils.m index 2ed69555c..c07f3dcbb 100644 --- a/Sources/BranchSDK/BNCEncodingUtils.m +++ b/Sources/BranchSDK/BNCEncodingUtils.m @@ -129,9 +129,8 @@ + (NSString *)sha256Encode:(NSString *)input { #pragma mark - Param Encoding methods + (NSString *)iso8601StringFromDate:(NSDate *)date { - static NSDateFormatter *dateFormatter; - static dispatch_once_t onceToken; - + static NSDateFormatter *dateFormatter = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]]; // POSIX to avoid weird issues diff --git a/Sources/BranchSDK/BNCEventUtils.m b/Sources/BranchSDK/BNCEventUtils.m index 911d60694..f65d90c22 100644 --- a/Sources/BranchSDK/BNCEventUtils.m +++ b/Sources/BranchSDK/BNCEventUtils.m @@ -15,8 +15,8 @@ @interface BNCEventUtils() @implementation BNCEventUtils + (instancetype)shared { - static BNCEventUtils *set; - static dispatch_once_t onceToken; + static BNCEventUtils *set = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ set = [BNCEventUtils new]; }); diff --git a/Sources/BranchSDK/BNCPartnerParameters.m b/Sources/BranchSDK/BNCPartnerParameters.m index c8642e488..e3048f8a0 100644 --- a/Sources/BranchSDK/BNCPartnerParameters.m +++ b/Sources/BranchSDK/BNCPartnerParameters.m @@ -16,8 +16,8 @@ @interface BNCPartnerParameters() @implementation BNCPartnerParameters + (instancetype)shared { - static BNCPartnerParameters *partnerParameters; - static dispatch_once_t onceToken; + static BNCPartnerParameters *partnerParameters = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ partnerParameters = [BNCPartnerParameters new]; }); diff --git a/Sources/BranchSDK/BNCPasteboard.m b/Sources/BranchSDK/BNCPasteboard.m index d1fe4765c..0ce70bb66 100644 --- a/Sources/BranchSDK/BNCPasteboard.m +++ b/Sources/BranchSDK/BNCPasteboard.m @@ -13,8 +13,8 @@ @implementation BNCPasteboard + (BNCPasteboard *)sharedInstance { - static BNCPasteboard *pasteboard; - static dispatch_once_t onceToken; + static BNCPasteboard *pasteboard = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ pasteboard = [BNCPasteboard new]; }); diff --git a/Sources/BranchSDK/BNCPreferenceHelper.m b/Sources/BranchSDK/BNCPreferenceHelper.m index 730fcbe1a..ff4eacafa 100644 --- a/Sources/BranchSDK/BNCPreferenceHelper.m +++ b/Sources/BranchSDK/BNCPreferenceHelper.m @@ -122,8 +122,8 @@ @implementation BNCPreferenceHelper adUserDataUsageConsent = _adUserDataUsageConsent; + (BNCPreferenceHelper *)sharedInstance { - static BNCPreferenceHelper *preferenceHelper; - static dispatch_once_t onceToken; + static BNCPreferenceHelper *preferenceHelper = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ preferenceHelper = [[BNCPreferenceHelper alloc] init]; diff --git a/Sources/BranchSDK/BNCQRCodeCache.m b/Sources/BranchSDK/BNCQRCodeCache.m index 2efa1996f..b77723e64 100644 --- a/Sources/BranchSDK/BNCQRCodeCache.m +++ b/Sources/BranchSDK/BNCQRCodeCache.m @@ -16,8 +16,8 @@ @implementation BNCQRCodeCache //Can only hold one QR code in cache. Just used to debounce. + (BNCQRCodeCache *) sharedInstance { - static BNCQRCodeCache *singleton; - static dispatch_once_t onceToken; + static BNCQRCodeCache *singleton = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ singleton = [BNCQRCodeCache new]; }); diff --git a/Sources/BranchSDK/BNCReachability.m b/Sources/BranchSDK/BNCReachability.m index d1516c785..ddc98e6e9 100644 --- a/Sources/BranchSDK/BNCReachability.m +++ b/Sources/BranchSDK/BNCReachability.m @@ -28,8 +28,8 @@ @interface BNCReachability() @implementation BNCReachability + (BNCReachability *)shared { - static BNCReachability *reachability; - static dispatch_once_t onceToken; + static BNCReachability *reachability = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ reachability = [BNCReachability new]; }); diff --git a/Sources/BranchSDK/BNCSKAdNetwork.m b/Sources/BranchSDK/BNCSKAdNetwork.m index e1de4b436..f5d05be60 100644 --- a/Sources/BranchSDK/BNCSKAdNetwork.m +++ b/Sources/BranchSDK/BNCSKAdNetwork.m @@ -27,8 +27,8 @@ @interface BNCSKAdNetwork() @implementation BNCSKAdNetwork + (BNCSKAdNetwork *)sharedInstance { - static BNCSKAdNetwork *singleton; - static dispatch_once_t onceToken; + static BNCSKAdNetwork *singleton = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ singleton = [[BNCSKAdNetwork alloc] init]; }); diff --git a/Sources/BranchSDK/BNCServerAPI.m b/Sources/BranchSDK/BNCServerAPI.m index 73f8cb253..15f40ff8e 100644 --- a/Sources/BranchSDK/BNCServerAPI.m +++ b/Sources/BranchSDK/BNCServerAPI.m @@ -14,8 +14,8 @@ @implementation BNCServerAPI + (BNCServerAPI *)sharedInstance { - static BNCServerAPI *serverAPI; - static dispatch_once_t onceToken; + static BNCServerAPI *serverAPI = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ serverAPI = [[BNCServerAPI alloc] init]; diff --git a/Sources/BranchSDK/BNCServerRequestQueue.m b/Sources/BranchSDK/BNCServerRequestQueue.m index 3beb7136e..28bdd988a 100755 --- a/Sources/BranchSDK/BNCServerRequestQueue.m +++ b/Sources/BranchSDK/BNCServerRequestQueue.m @@ -340,7 +340,7 @@ - (id)unarchiveObjectFromData:(NSData *)data { // only replay analytics requests, the others are time sensitive + (NSSet *)replayableRequestClasses { static NSSet *requestClasses = nil; - static dispatch_once_t onceToken; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^ { NSArray *tmp = @[ [BranchOpenRequest class], @@ -356,7 +356,7 @@ - (id)unarchiveObjectFromData:(NSData *)data { // encodable classes also includes NSArray and NSData + (NSSet *)encodableClasses { static NSSet *classes = nil; - static dispatch_once_t onceToken; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^ { NSMutableArray *tmp = [NSMutableArray new]; [tmp addObject:[NSArray class]]; // root object @@ -373,7 +373,7 @@ - (id)unarchiveObjectFromData:(NSData *)data { + (NSURL * _Nonnull) URLForQueueFile { static NSURL *URL = nil; - static dispatch_once_t onceToken; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^ { URL = BNCURLForBranchDirectory(); URL = [URL URLByAppendingPathComponent:BRANCH_QUEUE_FILE isDirectory:NO]; @@ -383,7 +383,7 @@ + (NSURL * _Nonnull) URLForQueueFile { + (instancetype)getInstance { static BNCServerRequestQueue *sharedQueue = nil; - static dispatch_once_t onceToken; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^ { sharedQueue = [[BNCServerRequestQueue alloc] init]; [sharedQueue retrieve]; diff --git a/Sources/BranchSDK/BNCUserAgentCollector.m b/Sources/BranchSDK/BNCUserAgentCollector.m index 029173f4c..d6a77f064 100644 --- a/Sources/BranchSDK/BNCUserAgentCollector.m +++ b/Sources/BranchSDK/BNCUserAgentCollector.m @@ -26,8 +26,8 @@ @interface BNCUserAgentCollector() @implementation BNCUserAgentCollector + (BNCUserAgentCollector *)instance { - static BNCUserAgentCollector *collector; - static dispatch_once_t onceToken; + static BNCUserAgentCollector *collector = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ collector = [BNCUserAgentCollector new]; }); diff --git a/Sources/BranchSDK/BranchContentDiscoverer.m b/Sources/BranchSDK/BranchContentDiscoverer.m index 6bf6ff24c..f186ebbd0 100644 --- a/Sources/BranchSDK/BranchContentDiscoverer.m +++ b/Sources/BranchSDK/BranchContentDiscoverer.m @@ -26,8 +26,8 @@ @interface BranchContentDiscoverer () @implementation BranchContentDiscoverer + (BranchContentDiscoverer *)getInstance { - static BranchContentDiscoverer *sharedInstance; - static dispatch_once_t onceToken; + static BranchContentDiscoverer *sharedInstance = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ sharedInstance = [BranchContentDiscoverer new]; }); diff --git a/Sources/BranchSDK/BranchContentDiscoveryManifest.m b/Sources/BranchSDK/BranchContentDiscoveryManifest.m index 0354b2174..fc5e6163d 100644 --- a/Sources/BranchSDK/BranchContentDiscoveryManifest.m +++ b/Sources/BranchSDK/BranchContentDiscoveryManifest.m @@ -34,8 +34,8 @@ - (instancetype)init { } + (BranchContentDiscoveryManifest *)getInstance { - static BranchContentDiscoveryManifest *sharedInstance; - static dispatch_once_t onceToken; + static BranchContentDiscoveryManifest *sharedInstance = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ sharedInstance = [BranchContentDiscoveryManifest new]; }); diff --git a/Sources/BranchSDK/BranchJsonConfig.m b/Sources/BranchSDK/BranchJsonConfig.m index 430c6b859..a27850daa 100644 --- a/Sources/BranchSDK/BranchJsonConfig.m +++ b/Sources/BranchSDK/BranchJsonConfig.m @@ -28,16 +28,13 @@ @interface BranchJsonConfig() @implementation BranchJsonConfig -+ (BranchJsonConfig * _Nonnull)instance -{ - @synchronized(self) { - static BranchJsonConfig *_instance; - static dispatch_once_t once = 0; - dispatch_once(&once, ^{ - _instance = [[BranchJsonConfig alloc] init]; - }); - return _instance; - } ++ (BranchJsonConfig * _Nonnull)instance { + static BranchJsonConfig *instance = nil; + static dispatch_once_t once = 0; + dispatch_once(&once, ^{ + instance = [BranchJsonConfig new]; + }); + return instance; } - (instancetype)init diff --git a/Sources/BranchSDK/BranchLogger.m b/Sources/BranchSDK/BranchLogger.m index 3f5038b57..d64a65568 100644 --- a/Sources/BranchSDK/BranchLogger.m +++ b/Sources/BranchSDK/BranchLogger.m @@ -31,7 +31,7 @@ - (instancetype)init { + (instancetype)shared { static BranchLogger *sharedInstance = nil; - static dispatch_once_t onceToken; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ sharedInstance = [[BranchLogger alloc] init]; sharedInstance.loggingEnabled = NO; diff --git a/Sources/BranchSDK/BranchPluginSupport.m b/Sources/BranchSDK/BranchPluginSupport.m index c975d930b..7b0af19d3 100644 --- a/Sources/BranchSDK/BranchPluginSupport.m +++ b/Sources/BranchSDK/BranchPluginSupport.m @@ -16,8 +16,8 @@ @implementation BranchPluginSupport + (BranchPluginSupport *)instance { - static BranchPluginSupport *pluginSupport; - static dispatch_once_t onceToken; + static BranchPluginSupport *pluginSupport = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ pluginSupport = [BranchPluginSupport new]; }); diff --git a/Sources/BranchSDK/BranchScene.m b/Sources/BranchSDK/BranchScene.m index c4f3a2f53..cb176994e 100644 --- a/Sources/BranchSDK/BranchScene.m +++ b/Sources/BranchSDK/BranchScene.m @@ -13,8 +13,8 @@ @implementation BranchScene + (BranchScene *)shared NS_EXTENSION_UNAVAILABLE("BranchScene does not support Extensions") { - static BranchScene *bscene; - static dispatch_once_t onceToken; + static BranchScene *bscene = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ bscene = [BranchScene new]; }); diff --git a/Sources/BranchSDK/NSError+Branch.m b/Sources/BranchSDK/NSError+Branch.m index 1ffe8bd9d..5e06a6987 100644 --- a/Sources/BranchSDK/NSError+Branch.m +++ b/Sources/BranchSDK/NSError+Branch.m @@ -22,8 +22,8 @@ + (NSString *)bncErrorDomain { // Legacy error messages + (NSString *)messageForCode:(BNCErrorCode)code { - static NSMutableDictionary *messages; - static dispatch_once_t onceToken; + static NSMutableDictionary *messages = nil; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ messages = [NSMutableDictionary new]; [messages setObject:@"The Branch user session has not been initialized." forKey:@(BNCInitError)]; From 346055c75325dc404616eec7c646f3c10d642bce Mon Sep 17 00:00:00 2001 From: echo Date: Thu, 4 Apr 2024 10:44:27 -0700 Subject: [PATCH 4/7] Remove unnecessary synchronization --- Sources/BranchSDK/BNCKeyChain.m | 58 ++++++++++++++++----------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/Sources/BranchSDK/BNCKeyChain.m b/Sources/BranchSDK/BNCKeyChain.m index 12dfaa640..52113779f 100644 --- a/Sources/BranchSDK/BNCKeyChain.m +++ b/Sources/BranchSDK/BNCKeyChain.m @@ -149,37 +149,35 @@ + (NSError*) removeValuesForService:(NSString *)service key:(NSString *)key { // The security access group string is prefixed with the Apple Developer Team ID + (NSString * _Nullable)securityAccessGroup { - @synchronized(self) { - static NSString *_securityAccessGroup = nil; - if (_securityAccessGroup) return _securityAccessGroup; - - // The keychain cannot be empty prior to requesting the security access group string. Add a tmp variable. - NSError *error = [self storeDate:[NSDate date] forService:@"BranchKeychainService" key:@"Temp" cloudAccessGroup:nil]; - if (error) { - [[BranchLogger shared] logWarning:@"Failed to store temp value" error:error]; - } - - NSDictionary* dictionary = @{ - (__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword, - (__bridge id)kSecAttrService: @"BranchKeychainService", - (__bridge id)kSecReturnAttributes: (__bridge id)kCFBooleanTrue, - (__bridge id)kSecAttrSynchronizable: (__bridge id)kSecAttrSynchronizableAny, - (__bridge id)kSecMatchLimit: (__bridge id)kSecMatchLimitOne - }; - CFDictionaryRef resultDictionary = NULL; - OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)dictionary, (CFTypeRef*)&resultDictionary); - if (status == errSecItemNotFound) return nil; - if (status != errSecSuccess) { - [[BranchLogger shared] logWarning:[NSString stringWithFormat:@"Failed to retrieve security access group"] error:[self errorWithKey:nil OSStatus:status]]; - return nil; - } - NSString *group = [(__bridge NSDictionary *)resultDictionary objectForKey:(__bridge NSString *)kSecAttrAccessGroup]; - if (group.length > 0) { - _securityAccessGroup = [group copy]; - } - CFRelease(resultDictionary); - return _securityAccessGroup; + static NSString *_securityAccessGroup = nil; + if (_securityAccessGroup) return _securityAccessGroup; + + // The keychain cannot be empty prior to requesting the security access group string. Add a tmp variable. + NSError *error = [self storeDate:[NSDate date] forService:@"BranchKeychainService" key:@"Temp" cloudAccessGroup:nil]; + if (error) { + [[BranchLogger shared] logWarning:@"Failed to store temp value" error:error]; + } + + NSDictionary* dictionary = @{ + (__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword, + (__bridge id)kSecAttrService: @"BranchKeychainService", + (__bridge id)kSecReturnAttributes: (__bridge id)kCFBooleanTrue, + (__bridge id)kSecAttrSynchronizable: (__bridge id)kSecAttrSynchronizableAny, + (__bridge id)kSecMatchLimit: (__bridge id)kSecMatchLimitOne + }; + CFDictionaryRef resultDictionary = NULL; + OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)dictionary, (CFTypeRef*)&resultDictionary); + if (status == errSecItemNotFound) return nil; + if (status != errSecSuccess) { + [[BranchLogger shared] logWarning:[NSString stringWithFormat:@"Failed to retrieve security access group"] error:[self errorWithKey:nil OSStatus:status]]; + return nil; + } + NSString *group = [(__bridge NSDictionary *)resultDictionary objectForKey:(__bridge NSString *)kSecAttrAccessGroup]; + if (group.length > 0) { + _securityAccessGroup = [group copy]; } + CFRelease(resultDictionary); + return _securityAccessGroup; } @end From 824c72b7c01bd6e1446175310e0a00e4ad753c81 Mon Sep 17 00:00:00 2001 From: echo Date: Mon, 22 Apr 2024 13:01:38 -0700 Subject: [PATCH 5/7] Revert removal of synchronized --- Sources/BranchSDK/BNCKeyChain.m | 58 +++++++++++++++++---------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/Sources/BranchSDK/BNCKeyChain.m b/Sources/BranchSDK/BNCKeyChain.m index 52113779f..12dfaa640 100644 --- a/Sources/BranchSDK/BNCKeyChain.m +++ b/Sources/BranchSDK/BNCKeyChain.m @@ -149,35 +149,37 @@ + (NSError*) removeValuesForService:(NSString *)service key:(NSString *)key { // The security access group string is prefixed with the Apple Developer Team ID + (NSString * _Nullable)securityAccessGroup { - static NSString *_securityAccessGroup = nil; - if (_securityAccessGroup) return _securityAccessGroup; - - // The keychain cannot be empty prior to requesting the security access group string. Add a tmp variable. - NSError *error = [self storeDate:[NSDate date] forService:@"BranchKeychainService" key:@"Temp" cloudAccessGroup:nil]; - if (error) { - [[BranchLogger shared] logWarning:@"Failed to store temp value" error:error]; - } - - NSDictionary* dictionary = @{ - (__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword, - (__bridge id)kSecAttrService: @"BranchKeychainService", - (__bridge id)kSecReturnAttributes: (__bridge id)kCFBooleanTrue, - (__bridge id)kSecAttrSynchronizable: (__bridge id)kSecAttrSynchronizableAny, - (__bridge id)kSecMatchLimit: (__bridge id)kSecMatchLimitOne - }; - CFDictionaryRef resultDictionary = NULL; - OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)dictionary, (CFTypeRef*)&resultDictionary); - if (status == errSecItemNotFound) return nil; - if (status != errSecSuccess) { - [[BranchLogger shared] logWarning:[NSString stringWithFormat:@"Failed to retrieve security access group"] error:[self errorWithKey:nil OSStatus:status]]; - return nil; - } - NSString *group = [(__bridge NSDictionary *)resultDictionary objectForKey:(__bridge NSString *)kSecAttrAccessGroup]; - if (group.length > 0) { - _securityAccessGroup = [group copy]; + @synchronized(self) { + static NSString *_securityAccessGroup = nil; + if (_securityAccessGroup) return _securityAccessGroup; + + // The keychain cannot be empty prior to requesting the security access group string. Add a tmp variable. + NSError *error = [self storeDate:[NSDate date] forService:@"BranchKeychainService" key:@"Temp" cloudAccessGroup:nil]; + if (error) { + [[BranchLogger shared] logWarning:@"Failed to store temp value" error:error]; + } + + NSDictionary* dictionary = @{ + (__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword, + (__bridge id)kSecAttrService: @"BranchKeychainService", + (__bridge id)kSecReturnAttributes: (__bridge id)kCFBooleanTrue, + (__bridge id)kSecAttrSynchronizable: (__bridge id)kSecAttrSynchronizableAny, + (__bridge id)kSecMatchLimit: (__bridge id)kSecMatchLimitOne + }; + CFDictionaryRef resultDictionary = NULL; + OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)dictionary, (CFTypeRef*)&resultDictionary); + if (status == errSecItemNotFound) return nil; + if (status != errSecSuccess) { + [[BranchLogger shared] logWarning:[NSString stringWithFormat:@"Failed to retrieve security access group"] error:[self errorWithKey:nil OSStatus:status]]; + return nil; + } + NSString *group = [(__bridge NSDictionary *)resultDictionary objectForKey:(__bridge NSString *)kSecAttrAccessGroup]; + if (group.length > 0) { + _securityAccessGroup = [group copy]; + } + CFRelease(resultDictionary); + return _securityAccessGroup; } - CFRelease(resultDictionary); - return _securityAccessGroup; } @end From fa0a8444cab653d35cfb86089483c248c5c7ab4c Mon Sep 17 00:00:00 2001 From: echo Date: Mon, 22 Apr 2024 13:06:29 -0700 Subject: [PATCH 6/7] Change to dispatch once. --- Sources/BranchSDK/BNCKeyChain.m | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Sources/BranchSDK/BNCKeyChain.m b/Sources/BranchSDK/BNCKeyChain.m index 12dfaa640..2f30f2ae2 100644 --- a/Sources/BranchSDK/BNCKeyChain.m +++ b/Sources/BranchSDK/BNCKeyChain.m @@ -149,9 +149,9 @@ + (NSError*) removeValuesForService:(NSString *)service key:(NSString *)key { // The security access group string is prefixed with the Apple Developer Team ID + (NSString * _Nullable)securityAccessGroup { - @synchronized(self) { - static NSString *_securityAccessGroup = nil; - if (_securityAccessGroup) return _securityAccessGroup; + static NSString *_securityAccessGroup = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ // The keychain cannot be empty prior to requesting the security access group string. Add a tmp variable. NSError *error = [self storeDate:[NSDate date] forService:@"BranchKeychainService" key:@"Temp" cloudAccessGroup:nil]; @@ -168,18 +168,23 @@ + (NSString * _Nullable)securityAccessGroup { }; CFDictionaryRef resultDictionary = NULL; OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)dictionary, (CFTypeRef*)&resultDictionary); - if (status == errSecItemNotFound) return nil; + + if (status == errSecItemNotFound) { + return; + } if (status != errSecSuccess) { [[BranchLogger shared] logWarning:[NSString stringWithFormat:@"Failed to retrieve security access group"] error:[self errorWithKey:nil OSStatus:status]]; - return nil; + return; } NSString *group = [(__bridge NSDictionary *)resultDictionary objectForKey:(__bridge NSString *)kSecAttrAccessGroup]; if (group.length > 0) { _securityAccessGroup = [group copy]; } CFRelease(resultDictionary); - return _securityAccessGroup; - } + }); + + return _securityAccessGroup; + } @end From 18368239ebdc53aa491655d20913a785afc31940 Mon Sep 17 00:00:00 2001 From: echo Date: Mon, 22 Apr 2024 13:10:40 -0700 Subject: [PATCH 7/7] Add default value, just cause we do elsewhere. --- Sources/BranchSDK/BNCKeyChain.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/BranchSDK/BNCKeyChain.m b/Sources/BranchSDK/BNCKeyChain.m index 2f30f2ae2..a8e0a3e56 100644 --- a/Sources/BranchSDK/BNCKeyChain.m +++ b/Sources/BranchSDK/BNCKeyChain.m @@ -150,7 +150,7 @@ + (NSError*) removeValuesForService:(NSString *)service key:(NSString *)key { // The security access group string is prefixed with the Apple Developer Team ID + (NSString * _Nullable)securityAccessGroup { static NSString *_securityAccessGroup = nil; - static dispatch_once_t onceToken; + static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ // The keychain cannot be empty prior to requesting the security access group string. Add a tmp variable.