Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK 2302 update singleton creation to dispatch once #1388

Merged
merged 7 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Branch-TestBed/Branch-SDK-Tests/BNCEncodingUtilsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions Sources/BranchSDK/BNCAppGroupsData.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
Expand Down
4 changes: 2 additions & 2 deletions Sources/BranchSDK/BNCAppleReceipt.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
Expand Down
4 changes: 2 additions & 2 deletions Sources/BranchSDK/BNCCallbackMap.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
Expand Down
2 changes: 1 addition & 1 deletion Sources/BranchSDK/BNCDeviceInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ @interface BNCDeviceInfo()
@implementation BNCDeviceInfo

+ (BNCDeviceInfo *)getInstance {
static BNCDeviceInfo *bnc_deviceInfo = 0;
static BNCDeviceInfo *bnc_deviceInfo = nil;
gdeluna-branch marked this conversation as resolved.
Show resolved Hide resolved
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
bnc_deviceInfo = [BNCDeviceInfo new];
Expand Down
5 changes: 2 additions & 3 deletions Sources/BranchSDK/BNCEncodingUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Sources/BranchSDK/BNCEventUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
@implementation BNCEventUtils

+ (instancetype)shared {
static BNCEventUtils *set;
static dispatch_once_t onceToken;
static BNCEventUtils *set = nil;
static dispatch_once_t onceToken = 0;

Check warning on line 19 in Sources/BranchSDK/BNCEventUtils.m

View check run for this annotation

Codecov / codecov/patch

Sources/BranchSDK/BNCEventUtils.m#L18-L19

Added lines #L18 - L19 were not covered by tests
dispatch_once(&onceToken, ^{
set = [BNCEventUtils new];
});
Expand Down
19 changes: 12 additions & 7 deletions Sources/BranchSDK/BNCKeyChain.m
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@

// 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 = 0;
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];
Expand All @@ -168,18 +168,23 @@
};
CFDictionaryRef resultDictionary = NULL;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)dictionary, (CFTypeRef*)&resultDictionary);
if (status == errSecItemNotFound) return nil;

if (status == errSecItemNotFound) {
return;

Check warning on line 173 in Sources/BranchSDK/BNCKeyChain.m

View check run for this annotation

Codecov / codecov/patch

Sources/BranchSDK/BNCKeyChain.m#L173

Added line #L173 was not covered by tests
}
if (status != errSecSuccess) {
[[BranchLogger shared] logWarning:[NSString stringWithFormat:@"Failed to retrieve security access group"] error:[self errorWithKey:nil OSStatus:status]];
return nil;
return;

Check warning on line 177 in Sources/BranchSDK/BNCKeyChain.m

View check run for this annotation

Codecov / codecov/patch

Sources/BranchSDK/BNCKeyChain.m#L177

Added line #L177 was not covered by tests
}
NSString *group = [(__bridge NSDictionary *)resultDictionary objectForKey:(__bridge NSString *)kSecAttrAccessGroup];
if (group.length > 0) {
_securityAccessGroup = [group copy];
}
CFRelease(resultDictionary);
return _securityAccessGroup;
}
});

return _securityAccessGroup;

}

@end
4 changes: 2 additions & 2 deletions Sources/BranchSDK/BNCPartnerParameters.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
Expand Down
4 changes: 2 additions & 2 deletions Sources/BranchSDK/BNCPasteboard.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
Expand Down
4 changes: 2 additions & 2 deletions Sources/BranchSDK/BNCPreferenceHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions Sources/BranchSDK/BNCQRCodeCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
Expand Down
4 changes: 2 additions & 2 deletions Sources/BranchSDK/BNCReachability.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
Expand Down
4 changes: 2 additions & 2 deletions Sources/BranchSDK/BNCSKAdNetwork.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
Expand Down
4 changes: 2 additions & 2 deletions Sources/BranchSDK/BNCServerAPI.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
8 changes: 4 additions & 4 deletions Sources/BranchSDK/BNCServerRequestQueue.m
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ - (id)unarchiveObjectFromData:(NSData *)data {
// only replay analytics requests, the others are time sensitive
+ (NSSet<Class> *)replayableRequestClasses {
static NSSet<Class> *requestClasses = nil;
static dispatch_once_t onceToken;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^ {
NSArray *tmp = @[
[BranchOpenRequest class],
Expand All @@ -356,7 +356,7 @@ - (id)unarchiveObjectFromData:(NSData *)data {
// encodable classes also includes NSArray and NSData
+ (NSSet<Class> *)encodableClasses {
static NSSet<Class> *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
Expand All @@ -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];
Expand All @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions Sources/BranchSDK/BNCUserAgentCollector.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
Expand Down
9 changes: 4 additions & 5 deletions Sources/BranchSDK/BranchContentDiscoverer.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ @implementation BranchContentDiscoverer

+ (BranchContentDiscoverer *)getInstance {
static BranchContentDiscoverer *sharedInstance = nil;
@synchronized (self) {
if (!sharedInstance) {
sharedInstance = [[BranchContentDiscoverer alloc] init];
}
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
gdeluna-branch marked this conversation as resolved.
Show resolved Hide resolved
sharedInstance = [BranchContentDiscoverer new];
});
return sharedInstance;
}
}

- (void) dealloc {
Expand Down
13 changes: 6 additions & 7 deletions Sources/BranchSDK/BranchContentDiscoveryManifest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
sharedInstance = [BranchContentDiscoveryManifest new];
});
return sharedInstance;
}

- (void)onBranchInitialised:(NSDictionary *)branchInitDict withUrl:(NSString *)referringURL {
Expand Down
17 changes: 7 additions & 10 deletions Sources/BranchSDK/BranchJsonConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/BranchSDK/BranchLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Sources/BranchSDK/BranchPluginSupport.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
Expand Down
4 changes: 2 additions & 2 deletions Sources/BranchSDK/BranchScene.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
Expand Down
4 changes: 2 additions & 2 deletions Sources/BranchSDK/NSError+Branch.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ + (NSString *)bncErrorDomain {

// Legacy error messages
+ (NSString *)messageForCode:(BNCErrorCode)code {
static NSMutableDictionary<NSNumber *, NSString *> *messages;
static dispatch_once_t onceToken;
static NSMutableDictionary<NSNumber *, NSString *> *messages = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
messages = [NSMutableDictionary<NSNumber *, NSString *> new];
[messages setObject:@"The Branch user session has not been initialized." forKey:@(BNCInitError)];
Expand Down
Loading