@@ -91,6 +91,7 @@ @interface Branch() <BranchDeepLinkingControllerCompletionDelegate, FABKit>
9191@property (copy , nonatomic ) callbackWithParams sessionInitWithParamsCallback;
9292@property (copy , nonatomic ) callbackWithBranchUniversalObject sessionInitWithBranchUniversalObjectCallback;
9393@property (assign , nonatomic ) NSInteger networkCount;
94+ @property (assign , nonatomic ) NSInteger asyncRequestCount;
9495@property (assign , nonatomic ) BOOL isInitialized;
9596@property (assign , nonatomic ) BOOL shouldCallSessionInitCallback;
9697@property (assign , nonatomic ) BOOL shouldAutomaticallyDeepLink;
@@ -175,6 +176,7 @@ - (id)initWithInterface:(BNCServerInterface *)interface queue:(BNCServerRequestQ
175176 _shouldCallSessionInitCallback = YES ;
176177 _processing_sema = dispatch_semaphore_create (1 );
177178 _networkCount = 0 ;
179+ _asyncRequestCount = 0 ;
178180 _deepLinkControllers = [[NSMutableDictionary alloc ] init ];
179181 _whiteListedSchemeList = [[NSMutableArray alloc ] init ];
180182 _useCookieBasedMatching = YES ;
@@ -370,9 +372,14 @@ - (void)initSessionWithLaunchOptions:(NSDictionary *)options isReferrable:(BOOL)
370372 if (![options.allKeys containsObject: UIApplicationLaunchOptionsURLKey] &&
371373 ![options.allKeys containsObject: UIApplicationLaunchOptionsUserActivityDictionaryKey]) {
372374
375+ self.asyncRequestCount = 0 ;
376+
373377 // If Facebook SDK is present, call deferred app link check here which will later on call initUserSession
374- if (![self checkFacebookAppLinks ] && ![self checkAppleSearchAdsAttribution ]) {
375-
378+ [self checkFacebookAppLinks ];
379+ // If developer opted in, call deferred apple search attribution API here which will later on call initUserSession
380+ [self checkAppleSearchAdsAttribution ];
381+
382+ if (self.asyncRequestCount == 0 ) {
376383 // If we're not looking for App Links or Apple Search Ads, initialize
377384 [self initUserSessionAndCallCallback: YES ];
378385 }
@@ -540,9 +547,11 @@ - (BOOL)checkAppleSearchAdsAttribution {
540547 id sharedClientInstance = ((id (*)(id , SEL ))[ADClientClass methodForSelector: sharedClient])(ADClientClass, sharedClient);
541548
542549 self.preferenceHelper .shouldWaitForInit = YES ;
550+ self.preferenceHelper .checkedAppleSearchAdAttribution = YES ;
551+ self.asyncRequestCount ++;
543552
544553 void (^__nullable completionBlock)(NSDictionary *attrDetails, NSError *error) = ^void (NSDictionary *__nullable attrDetails, NSError *__nullable error) {
545- self.preferenceHelper . shouldWaitForInit = NO ;
554+ self.asyncRequestCount -- ;
546555
547556 if (attrDetails && [attrDetails count ]) {
548557 self.preferenceHelper .appleSearchAdDetails = attrDetails;
@@ -567,6 +576,11 @@ - (BOOL)checkAppleSearchAdsAttribution {
567576 self.preferenceHelper .appleSearchAdDetails = testInfo;
568577 }
569578
579+ // if there's another async attribution check in flight, don't continue with init
580+ if (self.asyncRequestCount > 0 ) { return ; }
581+
582+ self.preferenceHelper .shouldWaitForInit = NO ;
583+
570584 dispatch_async (dispatch_get_main_queue (), ^{
571585 [self initUserSessionAndCallCallback: !self .isInitialized];
572586 });
@@ -594,10 +608,17 @@ - (BOOL)checkFacebookAppLinks {
594608
595609 if ([self .FBSDKAppLinkUtility methodForSelector: fetchDeferredAppLink]) {
596610 void (^__nullable completionBlock)(NSURL *appLink, NSError *error) = ^void (NSURL *__nullable appLink, NSError *__nullable error) {
611+ self.asyncRequestCount --;
612+
613+ // if there's another async attribution check in flight, don't continue with init
614+ if (self.asyncRequestCount > 0 ) { return ; }
615+
597616 self.preferenceHelper .shouldWaitForInit = NO ;
617+
598618 [self handleDeepLink: appLink];
599619 };
600620
621+ self.asyncRequestCount ++;
601622 self.preferenceHelper .checkedFacebookAppLinks = YES ;
602623 self.preferenceHelper .shouldWaitForInit = YES ;
603624
0 commit comments