Skip to content

Commit 266ab88

Browse files
aaustinE-B-Smith
authored andcommitted
Race condition patch (#535)
* Fix for potential race condition * Update race condition patch with cleaner logic
1 parent e3d2b7b commit 266ab88

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

Branch-SDK/Branch-SDK/Branch.m

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,16 @@ - (void)initSessionWithLaunchOptions:(NSDictionary *)options isReferrable:(BOOL)
330330

331331
- (void)initSessionWithLaunchOptions:(NSDictionary *)options isReferrable:(BOOL)isReferrable explicitlyRequestedReferrable:(BOOL)explicitlyRequestedReferrable automaticallyDisplayController:(BOOL)automaticallyDisplayController {
332332
self.shouldAutomaticallyDeepLink = automaticallyDisplayController;
333+
334+
// If the SDK is already initialized, this means that initSession is being called later in the app lifecycle
335+
// and that the developer is expecting to receive deep link parameters via the callback block immediately
336+
if (self.isInitialized) {
337+
[self initUserSessionAndCallCallback:YES];
338+
}
333339

340+
// The rest of this function assumes that initSession is being called BEFORE continueUserActivity and openUrl
341+
// in the application life cycle, and that the SDK is not yet initialized.
342+
334343
// Handle push notification on app launch
335344
if ([options objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
336345
id branchUrlFromPush = [options objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey][BRANCH_PUSH_NOTIFICATION_PAYLOAD_KEY];
@@ -340,13 +349,22 @@ - (void)initSessionWithLaunchOptions:(NSDictionary *)options isReferrable:(BOOL)
340349
}
341350

342351
if ([BNCSystemObserver getOSVersion].integerValue >= 8) {
343-
if (![options.allKeys containsObject:UIApplicationLaunchOptionsURLKey] && ![options.allKeys containsObject:UIApplicationLaunchOptionsUserActivityDictionaryKey]) {
344-
// If Facebook SDK is present, call deferred app link check here
352+
353+
// Handle case where there's no URI scheme or Universal Link
354+
if (![options.allKeys containsObject:UIApplicationLaunchOptionsURLKey] &&
355+
![options.allKeys containsObject:UIApplicationLaunchOptionsUserActivityDictionaryKey]) {
356+
357+
// If Facebook SDK is present, call deferred app link check here which will later on call initUserSession
345358
if (![self checkFacebookAppLinks] && ![self checkAppleSearchAdsAttribution]) {
359+
360+
// If we're not looking for App Links or Apple Search Ads, initialize
346361
[self initUserSessionAndCallCallback:YES];
347362
}
348363
}
364+
// Handle case where there is Universal Link present
349365
else if ([options.allKeys containsObject:UIApplicationLaunchOptionsUserActivityDictionaryKey]) {
366+
// Optional flag for the developer if they're letting Facebook return the boolean on didFinishLaunchingWithOptions
367+
// Note that this is no longer a recommended path, and that we tell developers to just return YES
350368
if (self.accountForFacebookSDK) {
351369
// does not work in Swift, because Objective-C to Swift interop is bad
352370
id activity = [[options objectForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey] objectForKey:@"UIApplicationLaunchOptionsUserActivityKey"];
@@ -355,6 +373,7 @@ - (void)initSessionWithLaunchOptions:(NSDictionary *)options isReferrable:(BOOL)
355373
return;
356374
}
357375
}
376+
// Wait for continueUserActivity Branch AppDelegate call to come through
358377
self.preferenceHelper.shouldWaitForInit = YES;
359378
}
360379
}

0 commit comments

Comments
 (0)