@@ -152,6 +152,11 @@ @interface Branch() <BranchDeepLinkingControllerCompletionDelegate> {
152152
153153@property (nonatomic , copy , nullable ) void (^sceneSessionInitWithCallback)(BNCInitSessionResponse * _Nullable initResponse, NSError * _Nullable error);
154154
155+ // Support for deferred SDK initialization. Used to support slow plugin runtime startup.
156+ // This is enabled by setting deferInitForPluginRuntime to true in branch.json
157+ @property (nonatomic , assign , readwrite ) BOOL deferInitForPluginRuntime;
158+ @property (nonatomic , copy , nullable ) void (^cachedInitBlock)(void );
159+
155160@end
156161
157162@implementation Branch
@@ -226,13 +231,14 @@ - (id)initWithInterface:(BNCServerInterface *)interface
226231 [self loadUserAgent ];
227232
228233 BranchJsonConfig *config = BranchJsonConfig.instance ;
234+ self.deferInitForPluginRuntime = config.deferInitForPluginRuntime ;
229235
230- if (config.checkPasteboardOnInstall ) {
231- [self checkPasteboardOnInstall ];
236+ if (config.enableLogging ) {
237+ [self enableLogging ];
232238 }
233239
234- if (config.delayInitToCheckForSearchAds ) {
235- [self delayInitToCheckForSearchAds ];
240+ if (config.checkPasteboardOnInstall ) {
241+ [self checkPasteboardOnInstall ];
236242 }
237243
238244 if (config.enableFacebookLinkCheck ) {
@@ -613,8 +619,10 @@ - (void)initSessionWithLaunchOptions:(NSDictionary *)options isReferrable:(BOOL)
613619
614620- (void )initSceneSessionWithLaunchOptions : (NSDictionary *)options isReferrable : (BOOL )isReferrable explicitlyRequestedReferrable : (BOOL )explicitlyRequestedReferrable automaticallyDisplayController : (BOOL )automaticallyDisplayController
615621 registerDeepLinkHandler : (void (^)(BNCInitSessionResponse * _Nullable initResponse, NSError * _Nullable error))callback {
616- self.sceneSessionInitWithCallback = callback;
617- [self initSessionWithLaunchOptions: options isReferrable: isReferrable explicitlyRequestedReferrable: explicitlyRequestedReferrable automaticallyDisplayController: automaticallyDisplayController];
622+ [self deferInitBlock: ^{
623+ self.sceneSessionInitWithCallback = callback;
624+ [self initSessionWithLaunchOptions: options isReferrable: isReferrable explicitlyRequestedReferrable: explicitlyRequestedReferrable automaticallyDisplayController: automaticallyDisplayController];
625+ }];
618626}
619627
620628- (void )initSessionWithLaunchOptions : (NSDictionary *)options
@@ -2136,9 +2144,36 @@ - (void)clearNetworkQueue {
21362144
21372145#pragma mark - Session Initialization
21382146
2147+ // Defers block until notifyNativeToInit is called.
2148+ - (BOOL )deferInitBlock : (void (^)(void ))block {
2149+ BOOL deferred = NO ;
2150+ @synchronized (self) {
2151+ if (self.deferInitForPluginRuntime ) {
2152+ self.cachedInitBlock = block;
2153+ deferred = YES ;
2154+ }
2155+ }
2156+
2157+ if (!deferred && block) {
2158+ block ();
2159+ }
2160+ return deferred;
2161+ }
2162+
2163+ // Releases deferred init block
2164+ - (void )notifyNativeToInit {
2165+ @synchronized (self) {
2166+ self.deferInitForPluginRuntime = NO ;
2167+ }
2168+
2169+ if (self.cachedInitBlock ) {
2170+ self.cachedInitBlock ();
2171+ }
2172+ self.cachedInitBlock = nil ;
2173+ }
2174+
21392175// SDK-631 Workaround to maintain existing error handling behavior.
21402176// Some methods require init before they are called. Instead of returning an error, we try to fix the situation by calling init ourselves.
2141- // There is a follow up ticket to improve this. SDK-633
21422177- (void )initSafetyCheck {
21432178 if (self.initializationStatus == BNCInitStatusUninitialized) {
21442179 BNCLogDebug (@" Branch avoided an error by preemptively initializing." );
@@ -2147,6 +2182,16 @@ - (void)initSafetyCheck {
21472182}
21482183
21492184- (void )initUserSessionAndCallCallback : (BOOL )callCallback sceneIdentifier : (NSString *)sceneIdentifier {
2185+
2186+ // ignore lifecycle calls while waiting for a plugin runtime.
2187+ @synchronized (self) {
2188+ if (self.deferInitForPluginRuntime ) {
2189+ // NSString *debug = [NSString stringWithFormat:@"Init is deferred, ignoring call: %@", NSThread.callStackSymbols];
2190+ // BNCLogDebug(debug);
2191+ return ;
2192+ }
2193+ }
2194+
21502195 dispatch_async (self.isolationQueue , ^(){
21512196 NSString *urlstring = nil ;
21522197 if (self.preferenceHelper .universalLinkUrl .length ) {
0 commit comments