Skip to content

Commit 9c747b8

Browse files
strip down dispatch pre init block to simply dispatching to isolationqueue and require management of the sephamore inside the passed in block
1 parent 31946fe commit 9c747b8

File tree

5 files changed

+45
-27
lines changed

5 files changed

+45
-27
lines changed

Branch-SDK-Tests/BNCPreInitBlockTests.m renamed to Branch-SDK-Tests/DispatchToIsolationQueueTests.m

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
#import <XCTest/XCTest.h>
1010
#import "Branch.h"
1111

12-
@interface BNCPreInitBlockTests : XCTestCase
12+
@interface DispatchToIsolationQueueTests : XCTestCase
1313
@property (nonatomic, strong, readwrite) Branch *branch;
1414
@property (nonatomic, strong, readwrite) BNCPreferenceHelper *prefHelper;
1515
@end
1616

17-
@implementation BNCPreInitBlockTests
17+
// this is an integration test, needs to be moved to a new target
18+
@implementation DispatchToIsolationQueueTests
1819

1920
- (void)setUp {
2021
self.branch = [Branch getInstance];
@@ -28,9 +29,14 @@ - (void)tearDown {
2829
- (void)testPreInitBlock {
2930
__block XCTestExpectation *expectation = [self expectationWithDescription:@""];
3031

31-
[self.branch dispatchPreInitBlock:^{
32-
[self.branch setRequestMetadataKey:@"$marketing_cloud_visitor_id" value:@"adobeID123"];
33-
} executeAfter:3];
32+
[self.branch dispatchToIsolationQueue:^{
33+
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
34+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
35+
[self.branch setRequestMetadataKey:@"$marketing_cloud_visitor_id" value:@"adobeID123"];
36+
dispatch_semaphore_signal(semaphore);
37+
});
38+
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
39+
}];
3440

3541
[self.branch initSessionWithLaunchOptions:nil andRegisterDeepLinkHandlerUsingBranchUniversalObject:
3642
^ (BranchUniversalObject * _Nullable universalObject,
@@ -46,7 +52,7 @@ - (void)testPreInitBlock {
4652
XCTAssertNil([[self.prefHelper requestMetadataDictionary] objectForKey:@"$marketing_cloud_visitor_id"]);
4753
});
4854

49-
// test that initialization does happen afterwards and that pre inti block was executed
55+
// test that initialization does happen afterwards and that pre init block was executed
5056
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
5157
id initializationStatus = [self.branch valueForKey:@"initializationStatus"];
5258
XCTAssertTrue([self enumIntValueFromId:initializationStatus] == 2);// initialized

Branch-SDK/Branch-SDK/Branch.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,13 +517,17 @@ typedef NS_ENUM(NSUInteger, BranchCreditHistoryOrder) {
517517
#pragma mark - Pre-initialization support
518518

519519
/**
520-
When certain actions are required to complete prior to session initialization, call this method passing in a dispatch_block_t
521-
object and an int, inidicating the number of seconds Branch should wait to invoke the passed in block.
520+
DO NOT USE unless you are familiar with the SDK's threading model.
521+
522+
When certain actions are required to complete prior to session initialization, this method can be used to pass in a blocking dispatch_block_t.
523+
The structure of the dispatch_block_t should be as follows:
524+
1. obtain semaphore
525+
2. initiate async task that signals the sephamore in its callback
526+
3. make sephamore wait for the signal
522527
523528
@param initBlock dispatch_block_t object to be executed prior to session initialization
524-
@param waitTime An int inidicating the number of seconds Branch should wait before executing the passed in block
525529
*/
526-
- (void)dispatchPreInitBlock:(dispatch_block_t)initBlock executeAfter:(int)waitTime;
530+
- (void)dispatchToIsolationQueue:(dispatch_block_t)initBlock;
527531

528532
#pragma mark - Push Notification support
529533

Branch-SDK/Branch-SDK/Branch.m

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -919,19 +919,8 @@ - (void)checkAppleSearchAdsAttribution {
919919

920920
#pragma mark - Pre-initialization support
921921

922-
- (void) dispatchPreInitBlock:(dispatch_block_t) initBlock executeAfter:(int)waitTime {
923-
dispatch_async(self.isolationQueue, ^(){
924-
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
925-
926-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(waitTime * NSEC_PER_SEC)), self.isolationQueue, ^{
927-
@try {
928-
initBlock();
929-
} @catch (NSException *ignored) {}
930-
dispatch_semaphore_signal(semaphore);
931-
});
932-
933-
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
934-
});
922+
- (void) dispatchToIsolationQueue:(dispatch_block_t) initBlock {
923+
dispatch_async(self.isolationQueue, initBlock);
935924
}
936925

937926
#pragma mark - Facebook App Link Check

Branch-TestBed/Branch-TestBed.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
4683F0751B20A73F00A432E7 /* CreditHistoryViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EAA790119E89F67008D4A83 /* CreditHistoryViewController.m */; };
2525
4683F0761B20A73F00A432E7 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 670016731940F51400A9E103 /* AppDelegate.m */; };
2626
46DC406E1B2A328900D2D203 /* AdSupport.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 67BBCF271A69E49A009C7DAE /* AdSupport.framework */; };
27-
4AB16368239E3A2700D42931 /* BNCPreInitBlockTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4AB16367239E3A2700D42931 /* BNCPreInitBlockTests.m */; };
27+
4AB16368239E3A2700D42931 /* DispatchToIsolationQueueTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4AB16367239E3A2700D42931 /* DispatchToIsolationQueueTests.m */; };
2828
4D130E731EE0C7B100A69A0A /* BNCNetworkService.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D130E4D1EE0C7B000A69A0A /* BNCNetworkService.m */; };
2929
4D130E761EE0C7B100A69A0A /* BNCServerInterface.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D130E501EE0C7B000A69A0A /* BNCServerInterface.m */; };
3030
4D130E781EE0C7B100A69A0A /* BNCServerRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D130E521EE0C7B000A69A0A /* BNCServerRequest.m */; };
@@ -302,7 +302,7 @@
302302
466D5A0F1B5991E3009DB845 /* BNCContentDiscoveryManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BNCContentDiscoveryManager.h; sourceTree = "<group>"; };
303303
466D5A101B5991E3009DB845 /* BNCContentDiscoveryManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BNCContentDiscoveryManager.m; sourceTree = "<group>"; };
304304
46DBB42F1B335A9B00642FC8 /* BranchDeepLinkingController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BranchDeepLinkingController.h; sourceTree = "<group>"; };
305-
4AB16367239E3A2700D42931 /* BNCPreInitBlockTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BNCPreInitBlockTests.m; sourceTree = "<group>"; };
305+
4AB16367239E3A2700D42931 /* DispatchToIsolationQueueTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DispatchToIsolationQueueTests.m; sourceTree = "<group>"; };
306306
4D130E4C1EE0C7B000A69A0A /* BNCNetworkService.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BNCNetworkService.h; sourceTree = "<group>"; };
307307
4D130E4D1EE0C7B000A69A0A /* BNCNetworkService.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BNCNetworkService.m; sourceTree = "<group>"; };
308308
4D130E4E1EE0C7B000A69A0A /* BNCNetworkServiceProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BNCNetworkServiceProtocol.h; sourceTree = "<group>"; };
@@ -724,7 +724,7 @@
724724
5F3D6719233062FD00454FF1 /* BNCJsonLoader.h */,
725725
5F3D671A233062FD00454FF1 /* BNCJsonLoader.m */,
726726
4D16839F2098C901008819E3 /* Info.plist */,
727-
4AB16367239E3A2700D42931 /* BNCPreInitBlockTests.m */,
727+
4AB16367239E3A2700D42931 /* DispatchToIsolationQueueTests.m */,
728728
);
729729
name = "Branch-SDK-Tests";
730730
path = "../Branch-SDK-Tests";
@@ -1401,7 +1401,7 @@
14011401
4D1683B92098C902008819E3 /* BNCSystemObserver.Test.m in Sources */,
14021402
4D1683BE2098C902008819E3 /* BranchShortUrlSyncRequestTests.m in Sources */,
14031403
4D1683CA2098C902008819E3 /* BNCPreferenceHelperTests.m in Sources */,
1404-
4AB16368239E3A2700D42931 /* BNCPreInitBlockTests.m in Sources */,
1404+
4AB16368239E3A2700D42931 /* DispatchToIsolationQueueTests.m in Sources */,
14051405
4D1683B32098C902008819E3 /* BranchGetCreditHistoryRequestTests.m in Sources */,
14061406
5F892ECE23624E0A0023AEC1 /* BNCFacebookAppLinksTests.m in Sources */,
14071407
4D1683BB2098C902008819E3 /* BranchShortUrlRequestTests.m in Sources */,

Branch-TestBed/Branch-TestBed/AppDelegate.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ - (BOOL)application:(UIApplication *)application
2929

3030
//
3131
Branch *branch = [Branch getInstance];
32+
33+
34+
// test pre init support
35+
// [self testDispatchToIsolationQueue:branch]
3236

3337
// Comment out (for match guarantee testing) / or un-comment to toggle debugging:
3438
// Note: Unit tests will fail if 'setDebug' is set.
@@ -57,6 +61,21 @@ - (BOOL)application:(UIApplication *)application
5761
return YES;
5862
}
5963

64+
// pre init support is meant for extensions, for exmaple, when Adobe axtension needs to pass in Adobe IDs
65+
// before init session is called. This method will block the queue used by open/install requests until the
66+
// the passsed in block completes
67+
- (void) testDispatchToIsolationQueue:(Branch*)branch {
68+
[branch dispatchToIsolationQueue:^{
69+
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
70+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),
71+
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
72+
[branch setRequestMetadataKey:@"keykey" value:@"valuevalue"];
73+
dispatch_semaphore_signal(semaphore);
74+
});
75+
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
76+
}];
77+
}
78+
6079
- (void) handleDeepLinkParams:(NSDictionary*)params error:(NSError*)error {
6180
if (error) {
6281
NSLog(@"Branch TestBed: Error deep linking: %@.", error.localizedDescription);

0 commit comments

Comments
 (0)