Skip to content

Commit f429664

Browse files
committed
SDK-1831 move feature to Branch class
1 parent 67e6dce commit f429664

File tree

4 files changed

+48
-85
lines changed

4 files changed

+48
-85
lines changed

BranchSDK/Branch.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,16 @@ typedef NS_ENUM(NSUInteger, BranchCreditHistoryOrder) {
528528
*/
529529
- (void)dispatchToIsolationQueue:(dispatch_block_t)initBlock;
530530

531+
/**
532+
DO NOT USE unless you are implementing deferred initialization for plugins.
533+
534+
Platforms such as React Native and Unity, have slow runtime startups. This results in early lifecycle events before client code can run.
535+
When `deferInitForPlugin` is true in `branch.json` initSession with cache itself until this method is called.
536+
537+
Note that while init is deferred, other calls to the Branch SDK may result in errors. For that reason, do not use this feature for general SDK init deferral.
538+
*/
539+
- (void)notifyNativeToInit;
540+
531541
#pragma mark - Push Notification support
532542

533543
/**

BranchSDK/Branch.m

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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 deferInitForPlugin to true in branch.json
157+
@property (nonatomic, assign, readwrite) BOOL deferInitForPlugin;
158+
@property (nonatomic, copy, nullable) void (^cachedInitBlock)(void);
159+
155160
@end
156161

157162
@implementation Branch
@@ -226,6 +231,7 @@ - (id)initWithInterface:(BNCServerInterface *)interface
226231
[self loadUserAgent];
227232

228233
BranchJsonConfig *config = BranchJsonConfig.instance;
234+
self.deferInitForPlugin = config.deferInitForPlugin;
229235

230236
if (config.enableLogging) {
231237
[self enableLogging];
@@ -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,6 +2144,34 @@ - (void)clearNetworkQueue {
21362144

21372145
#pragma mark - Session Initialization
21382146

2147+
// Defers block until notifyNativeToInit is called. Also blocks autoinitialization by initSafetyCheck.
2148+
- (BOOL)deferInitBlock:(void (^)(void))block {
2149+
BOOL deferred = NO;
2150+
@synchronized (self) {
2151+
if (self.deferInitForPlugin) {
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.deferInitForPlugin = 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.
21412177
// There is a follow up ticket to improve this. SDK-633

BranchSDK/BranchPluginSupport.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,13 @@
77
//
88

99
#import <Foundation/Foundation.h>
10-
#import "BranchUniversalObject.h"
1110

1211
NS_ASSUME_NONNULL_BEGIN
1312

1413
@interface BranchPluginSupport : NSObject
1514

1615
+ (BranchPluginSupport *)instance;
1716

18-
#pragma mark - SDK entry points
19-
20-
- (void)initSessionWithLaunchOptions:(nullable NSDictionary *)options registerDeepLinkHandler:(void (^)(NSDictionary * _Nullable params, NSError * _Nullable error))callback;
21-
22-
- (void)initSessionWithLaunchOptions:(nullable NSDictionary *)options registerDeepLinkHandlerUsingBranchUniversalObject:(void (^)(BranchUniversalObject * _Nullable universalObject, BranchLinkProperties * _Nullable linkProperties, NSError * _Nullable error))callback;
23-
24-
- (BOOL)handleDeepLink:(nullable NSURL *)url;
25-
26-
- (BOOL)continueUserActivity:(nullable NSUserActivity *)userActivity;
27-
28-
- (void)notifyNativeToInit;
29-
30-
#pragma mark - Utility methods
31-
3217
- (NSDictionary<NSString *, NSString *> *)deviceDescription;
3318

3419
@end

BranchSDK/BranchPluginSupport.m

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@
1111
#import "BNCDeviceInfo.h"
1212
#import "BNCPreferenceHelper.h"
1313
#import "Branch.h"
14-
#import "BranchJsonConfig.h"
15-
16-
@interface BranchPluginSupport()
17-
18-
@property (nonatomic, assign, readwrite) BOOL deferInitForPlugin;
19-
@property (nonatomic, copy, nullable) void (^cachedBlock)(void);
20-
21-
@end
2214

2315
@implementation BranchPluginSupport
2416

@@ -31,66 +23,6 @@ + (BranchPluginSupport *)instance {
3123
return pluginSupport;
3224
}
3325

34-
- (instancetype)init {
35-
self = [super init];
36-
if (self) {
37-
self.deferInitForPlugin = [BranchJsonConfig instance].deferInitForPlugin;
38-
}
39-
return self;
40-
}
41-
42-
- (void)initSessionWithLaunchOptions:(nullable NSDictionary *)options registerDeepLinkHandler:(void (^)(NSDictionary * _Nullable params, NSError * _Nullable error))callback {
43-
[self deferBlock:^{
44-
[[Branch getInstance] initSessionWithLaunchOptions:options andRegisterDeepLinkHandler:callback];
45-
}];
46-
}
47-
48-
- (void)initSessionWithLaunchOptions:(nullable NSDictionary *)options registerDeepLinkHandlerUsingBranchUniversalObject:(void (^)(BranchUniversalObject * _Nullable universalObject, BranchLinkProperties * _Nullable linkProperties, NSError * _Nullable error))callback {
49-
[self deferBlock:^{
50-
[[Branch getInstance] initSessionWithLaunchOptions:options andRegisterDeepLinkHandlerUsingBranchUniversalObject:callback];
51-
}];
52-
}
53-
54-
- (BOOL)handleDeepLink:(nullable NSURL *)url {
55-
[self deferBlock:^{
56-
[[Branch getInstance] handleDeepLink:url sceneIdentifier:nil];
57-
}];
58-
return YES;
59-
}
60-
61-
- (BOOL)continueUserActivity:(nullable NSUserActivity *)userActivity {
62-
[self deferBlock:^{
63-
[[Branch getInstance] continueUserActivity:userActivity sceneIdentifier:nil];
64-
}];
65-
return YES;
66-
}
67-
68-
- (BOOL)deferBlock:(void (^)(void))block {
69-
BOOL deferred = NO;
70-
@synchronized (self) {
71-
if (self.deferInitForPlugin) {
72-
self.cachedBlock = block;
73-
deferred = YES;
74-
}
75-
}
76-
77-
if (!deferred && block) {
78-
block();
79-
}
80-
return deferred;
81-
}
82-
83-
- (void)notifyNativeToInit {
84-
@synchronized (self) {
85-
self.deferInitForPlugin = NO;
86-
}
87-
88-
if (self.cachedBlock) {
89-
self.cachedBlock();
90-
}
91-
self.cachedBlock = nil;
92-
}
93-
9426
- (NSDictionary<NSString *, NSString *> *)deviceDescription {
9527
NSMutableDictionary<NSString *, NSString *> *dictionary = [NSMutableDictionary new];
9628
BNCDeviceInfo *deviceInfo = [BNCDeviceInfo getInstance];

0 commit comments

Comments
 (0)