Skip to content

Commit 585a6f8

Browse files
added delayed initialization methods
1 parent 93af5bc commit 585a6f8

File tree

3 files changed

+74
-12
lines changed

3 files changed

+74
-12
lines changed

Branch-SDK/Branch-SDK/Branch.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,35 @@ typedef NS_ENUM(NSUInteger, BranchCreditHistoryOrder) {
514514
/// @name Push Notification Support
515515
///--------------------------------
516516

517+
#pragma mark - Delayed Initialization
518+
519+
/**
520+
When certain actions are required to complete prior to Branch initialization, call this method passing in dispatch_block_t,
521+
initBlock, which contains the desired initialization of Branch (i.e. any of the initSessionWithLaunchOptions functions) and an int,
522+
waitTime, which inidicates the number of seconds Branch should wait for the user to call invokeDelayedInitialization, after this
523+
time, the initialization block will be invoked automatically.
524+
525+
@param initBlock dispatch_block_t object that contains one of the initSessionWithLaunchOptions functions.
526+
@param waitTime An int inidicating the number of seconds Branch should wait for the user to call
527+
invokeDelayedInitialization before invoke the initBlock itself.
528+
@warning To avoid memory leaks take care to ensure that initBlock object does not capture any resources
529+
that require execution of the block body in order to be released, such as memory allocated with malloc(3)
530+
on which the block body calls free(3).
531+
*/
532+
- (void)dispatchInit:(dispatch_block_t)initBlock After:(int)waitTime;
533+
534+
/**
535+
Call this method if delayed initilization is no longer desired.
536+
537+
@warning Does not affect an initialization that is already in progress.
538+
*/
539+
- (void)cancelDelayedInitialization;
540+
541+
/**
542+
Used together with dispatchInitAfter, call this method after prerequisite tasks for Branch initialization have completed.
543+
*/
544+
- (void)invokeDelayedInitialization;
545+
517546
#pragma mark - Push Notification support
518547

519548
/**

Branch-SDK/Branch-SDK/Branch.m

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ @interface Branch() <BranchDeepLinkingControllerCompletionDelegate> {
119119
}
120120

121121
// This isolation queue protects branch initialization and ensures things are processed in order.
122-
@property (nonatomic, strong, readwrite) dispatch_queue_t isolationQueue;
122+
@property (nonatomic, strong) dispatch_queue_t isolationQueue;
123+
// Delayed initialization block to be used when other tasks are required to complete before initializing
124+
@property (nonatomic, strong) dispatch_block_t delayedInitBlock;
123125

124126
@property (strong, nonatomic) BNCServerInterface *serverInterface;
125127
@property (strong, nonatomic) BNCServerRequestQueue *requestQueue;
@@ -917,6 +919,27 @@ - (void)checkAppleSearchAdsAttribution {
917919
});
918920
}
919921

922+
#pragma mark - Delayed initialization
923+
924+
- (void) dispatchInit:(dispatch_block_t) initBlock After:(int)waitTime {
925+
self.delayedInitBlock = initBlock;
926+
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(waitTime * NSEC_PER_SEC));
927+
dispatch_after(time, dispatch_get_main_queue(), self.delayedInitBlock);
928+
}
929+
930+
- (void)cancelDelayedInitialization {
931+
// does not affect execution of the block object that is already in progress,
932+
// but it does prevent future invocation of the same block
933+
dispatch_block_cancel(self.delayedInitBlock);
934+
NSLog(@"BranchSDK: cancelDelayedInitialization %@", @"success");
935+
}
936+
937+
- (void)invokeDelayedInitialization {
938+
self.delayedInitBlock();
939+
[self cancelDelayedInitialization];
940+
NSLog(@"BranchSDK: invokeDelayedInitialization %@", @"success");
941+
}
942+
920943
#pragma mark - Facebook App Link Check
921944

922945
- (void)registerFacebookDeepLinkingClass:(id)FBSDKAppLinkUtility {
@@ -1775,12 +1798,12 @@ - (void)registerViewWithParams:(NSDictionary *)params andCallback:(callbackWithP
17751798
#pragma mark - Application State Change methods
17761799

17771800
- (void)applicationDidBecomeActive {
1778-
if (!Branch.trackingDisabled) {
1779-
if ((self.initializationStatus != BNCInitStatusInitialized) &&
1780-
![self.requestQueue containsInstallOrOpen]) {
1781-
[self initUserSessionAndCallCallback:YES];
1782-
}
1783-
}
1801+
// if (!Branch.trackingDisabled) {
1802+
// if ((self.initializationStatus != BNCInitStatusInitialized) &&
1803+
// ![self.requestQueue containsInstallOrOpen]) {
1804+
// [self initUserSessionAndCallCallback:YES];
1805+
// }
1806+
// }
17841807
}
17851808

17861809
- (void)applicationWillResignActive {

Branch-TestBed/Branch-TestBed/AppDelegate.m

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,21 @@ - (BOOL)application:(UIApplication *)application
4545
*/
4646

4747
// [branch setIdentity:@"Bobby Branch"];
48-
[branch initSessionWithLaunchOptions:launchOptions
49-
andRegisterDeepLinkHandlerUsingBranchUniversalObject:
50-
^ (BranchUniversalObject * _Nullable universalObject, BranchLinkProperties * _Nullable linkProperties, NSError * _Nullable error) {
51-
[self handleDeepLinkObject:universalObject linkProperties:linkProperties error:error];
52-
}];
48+
dispatch_block_t initBlock = dispatch_block_create(DISPATCH_BLOCK_ASSIGN_CURRENT, ^{
49+
[branch initSessionWithLaunchOptions:launchOptions
50+
andRegisterDeepLinkHandlerUsingBranchUniversalObject:
51+
^ (BranchUniversalObject * _Nullable universalObject, BranchLinkProperties * _Nullable linkProperties, NSError * _Nullable error) {
52+
[self handleDeepLinkObject:universalObject linkProperties:linkProperties error:error];
53+
}];
54+
NSLog(@"BranchSDK: initBlock has been invoked %@", @"success");
55+
});
56+
[branch dispatchInit:initBlock After:5];
57+
58+
// pretend that something prerequisite task completed in 2 seconds
59+
dispatch_time_t twoSecFromNow = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC));
60+
dispatch_after(twoSecFromNow, dispatch_get_main_queue(), ^{
61+
[branch invokeDelayedInitialization];
62+
});
5363

5464
// Push notification support (Optional)
5565
[self registerForPushNotifications:application];

0 commit comments

Comments
 (0)