Skip to content

Commit 64bff23

Browse files
simplified to block that executed before initsession, no invoke, no cancel
1 parent d256bca commit 64bff23

File tree

8 files changed

+178
-149
lines changed

8 files changed

+178
-149
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//
2+
// BNCDelayedInitSessionTests.m
3+
// Branch-SDK-Tests
4+
//
5+
// Created by Benas Klastaitis on 12/3/19.
6+
// Copyright © 2019 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import "Branch.h"
11+
12+
@interface BNCDelayedInitSessionTests : XCTestCase
13+
@property (nonatomic, strong, readwrite) Branch *branch;
14+
@property (nonatomic, strong, readwrite) BNCPreferenceHelper *prefHelper;
15+
@end
16+
17+
@implementation BNCDelayedInitSessionTests
18+
19+
20+
- (void)setUp {
21+
self.branch = [Branch getInstance];
22+
self.prefHelper = [[BNCPreferenceHelper alloc] init];
23+
}
24+
25+
- (void)tearDown {
26+
[self.prefHelper setRequestMetadataKey:@"$marketing_cloud_visitor_id" value:@"dummy"];
27+
}
28+
29+
- (void)testDispatchInitSession {
30+
__block XCTestExpectation *expectation = [self expectationWithDescription:@""];
31+
32+
[self.branch dispatchPreInitBlock:^{
33+
[self.branch setRequestMetadataKey:@"$marketing_cloud_visitor_id" value:@"adobeID123"];
34+
} executeAfter:3];
35+
36+
[self.branch initSessionWithLaunchOptions:nil andRegisterDeepLinkHandlerUsingBranchUniversalObject:
37+
^ (BranchUniversalObject * _Nullable universalObject,
38+
BranchLinkProperties * _Nullable linkProperties,
39+
NSError * _Nullable error) {
40+
[expectation fulfill];
41+
}];
42+
43+
// test that session initialization blocking works
44+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
45+
id initializationStatus = [self.branch valueForKey:@"initializationStatus"];
46+
XCTAssertTrue([self enumIntValueFromId:initializationStatus] == 0);// uninitialized
47+
XCTAssertNil([[self.prefHelper requestMetadataDictionary] objectForKey:@"$marketing_cloud_visitor_id"]);
48+
});
49+
50+
// test that initialization does happen afterwards and that pre inti block was executed
51+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
52+
id initializationStatus = [self.branch valueForKey:@"initializationStatus"];
53+
XCTAssertTrue([self enumIntValueFromId:initializationStatus] == 2);// initialized
54+
55+
XCTAssertTrue([[[self.prefHelper requestMetadataDictionary] objectForKey:@"$marketing_cloud_visitor_id"] isEqualToString:@"adobeID123"]);
56+
});
57+
58+
59+
[self waitForExpectationsWithTimeout:6 handler:^(NSError * _Nullable error) {
60+
NSLog(@"%@", error);
61+
}];
62+
}
63+
64+
-(int)enumIntValueFromId:(id)enumValueId {
65+
if (![enumValueId respondsToSelector:@selector(intValue)])
66+
return -1;
67+
68+
return [enumValueId intValue];
69+
}
70+
71+
@end
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// BNCPreInitBlockTests.m
3+
// Branch-SDK-Tests
4+
//
5+
// Created by Benas Klastaitis on 12/9/19.
6+
// Copyright © 2019 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import "Branch.h"
11+
12+
@interface BNCPreInitBlockTests : XCTestCase
13+
@property (nonatomic, strong, readwrite) Branch *branch;
14+
@property (nonatomic, strong, readwrite) BNCPreferenceHelper *prefHelper;
15+
@end
16+
17+
@implementation BNCPreInitBlockTests
18+
19+
- (void)setUp {
20+
self.branch = [Branch getInstance];
21+
self.prefHelper = [[BNCPreferenceHelper alloc] init];
22+
}
23+
24+
- (void)tearDown {
25+
[self.prefHelper setRequestMetadataKey:@"$marketing_cloud_visitor_id" value:@"dummy"];
26+
}
27+
28+
- (void)testDispatchInitSession {
29+
__block XCTestExpectation *expectation = [self expectationWithDescription:@""];
30+
31+
[self.branch dispatchPreInitBlock:^{
32+
[self.branch setRequestMetadataKey:@"$marketing_cloud_visitor_id" value:@"adobeID123"];
33+
} executeAfter:3];
34+
35+
[self.branch initSessionWithLaunchOptions:nil andRegisterDeepLinkHandlerUsingBranchUniversalObject:
36+
^ (BranchUniversalObject * _Nullable universalObject,
37+
BranchLinkProperties * _Nullable linkProperties,
38+
NSError * _Nullable error) {
39+
[expectation fulfill];
40+
}];
41+
42+
// test that session initialization blocking works
43+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
44+
id initializationStatus = [self.branch valueForKey:@"initializationStatus"];
45+
XCTAssertTrue([self enumIntValueFromId:initializationStatus] == 0);// uninitialized
46+
XCTAssertNil([[self.prefHelper requestMetadataDictionary] objectForKey:@"$marketing_cloud_visitor_id"]);
47+
});
48+
49+
// test that initialization does happen afterwards and that pre inti block was executed
50+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
51+
id initializationStatus = [self.branch valueForKey:@"initializationStatus"];
52+
XCTAssertTrue([self enumIntValueFromId:initializationStatus] == 2);// initialized
53+
54+
XCTAssertTrue([[[self.prefHelper requestMetadataDictionary] objectForKey:@"$marketing_cloud_visitor_id"] isEqualToString:@"adobeID123"]);
55+
});
56+
57+
58+
[self waitForExpectationsWithTimeout:6 handler:^(NSError * _Nullable error) {
59+
NSLog(@"%@", error);
60+
}];
61+
}
62+
63+
-(int)enumIntValueFromId:(id)enumValueId {
64+
if (![enumValueId respondsToSelector:@selector(intValue)])
65+
return -1;
66+
67+
return [enumValueId intValue];
68+
}
69+
70+
@end

Branch-SDK/Branch-SDK/Branch.h

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

517-
#pragma mark - Delayed Initialization
517+
#pragma mark - Pre-initialization support
518518

519519
/**
520520
When certain actions are required to complete prior to session initialization, call this method passing in dispatch_block_t,
521-
which contains session initialization, and an int, inidicating the number of seconds Branch should wait for the user
522-
to invoke the delayed Branch session initialization (see invokeDelayedInitialization). After this time, Branch will automatically
523-
invoke the delayed session initialization.
524-
525-
@param initBlock dispatch_block_t object with Branch initialization in it
526-
@param waitTime An int inidicating the number of seconds Branch should wait for the user to
527-
invoke the delayed initialization.
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)dispatchInitSession:(dispatch_block_t)initBlock After:(int)waitTime;
533-
534-
/**
535-
Call this method if delayed initilization is no longer desired.
521+
which contains session initialization, and an int, inidicating the number of seconds Branch should wait to invoke the
522+
passed in block.
536523
537-
@warning Does not affect an initialization that is already in progress.
538-
*/
539-
- (void)cancelDelayedInitSession;
540-
541-
/**
542-
Used together with dispatchInitAfter, call this method after prerequisite tasks for Branch initialization have completed.
524+
@param initBlock dispatch_block_t object to be executed prior to session initialization
525+
@param waitTime An int inidicating the number of seconds Branch should wait before executing the passed in block
543526
*/
544-
- (void)invokeDelayedInitSession;
527+
- (void)dispatchPreInitBlock:(dispatch_block_t)initBlock executeAfter:(int)waitTime;
545528

546529
#pragma mark - Push Notification support
547530

Branch-SDK/Branch-SDK/Branch.m

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ @interface Branch() <BranchDeepLinkingControllerCompletionDelegate> {
121121
// This isolation queue protects branch initialization and ensures things are processed in order.
122122
@property (nonatomic, strong) dispatch_queue_t isolationQueue;
123123
// Delayed initialization block to be used when other tasks are required to complete before initializing
124-
@property (nonatomic, strong) dispatch_block_t delayedInitBlock;
124+
@property (nonatomic, strong) dispatch_block_t preInitBlock;
125125

126126
@property (strong, nonatomic) BNCServerInterface *serverInterface;
127127
@property (strong, nonatomic) BNCServerRequestQueue *requestQueue;
@@ -919,25 +919,21 @@ - (void)checkAppleSearchAdsAttribution {
919919
});
920920
}
921921

922-
#pragma mark - Delayed initialization
922+
#pragma mark - Pre-initialization support
923923

924-
- (void) dispatchInitSession:(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)cancelDelayedInitSession {
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)invokeDelayedInitSession {
938-
self.delayedInitBlock();
939-
[self cancelDelayedInitSession];
940-
NSLog(@"BranchSDK: invokeDelayedInitialization %@", @"success");
924+
- (void) dispatchPreInitBlock:(dispatch_block_t) initBlock executeAfter:(int)waitTime {
925+
dispatch_async(self.isolationQueue, ^(){
926+
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
927+
928+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(waitTime * NSEC_PER_SEC)), self.isolationQueue, ^{
929+
@try {
930+
initBlock();
931+
} @catch (NSException *ignored) {}
932+
dispatch_semaphore_signal(semaphore);
933+
});
934+
935+
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
936+
});
941937
}
942938

943939
#pragma mark - Facebook App Link Check
@@ -1800,8 +1796,7 @@ - (void)registerViewWithParams:(NSDictionary *)params andCallback:(callbackWithP
18001796
- (void)applicationDidBecomeActive {
18011797
if (!Branch.trackingDisabled &&
18021798
self.initializationStatus != BNCInitStatusInitialized &&
1803-
[self.requestQueue containsInstallOrOpen] &&
1804-
self.delayedInitBlock == nil) {
1799+
[self.requestQueue containsInstallOrOpen]) {
18051800
[self initUserSessionAndCallCallback:YES];
18061801
}
18071802
}

Branch-TestBed/Branch-SDK-Unhosted-Tests/BNCDelayedInitSessionTests.m

Lines changed: 0 additions & 101 deletions
This file was deleted.

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-
4A0C5F982397176500AE6628 /* BNCDelayedInitSessionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4A0C5F942397006800AE6628 /* BNCDelayedInitSessionTests.m */; };
27+
4AB16368239E3A2700D42931 /* BNCPreInitBlockTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4AB16367239E3A2700D42931 /* BNCPreInitBlockTests.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-
4A0C5F942397006800AE6628 /* BNCDelayedInitSessionTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BNCDelayedInitSessionTests.m; sourceTree = "<group>"; };
305+
4AB16367239E3A2700D42931 /* BNCPreInitBlockTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BNCPreInitBlockTests.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,6 +724,7 @@
724724
5F3D6719233062FD00454FF1 /* BNCJsonLoader.h */,
725725
5F3D671A233062FD00454FF1 /* BNCJsonLoader.m */,
726726
4D16839F2098C901008819E3 /* Info.plist */,
727+
4AB16367239E3A2700D42931 /* BNCPreInitBlockTests.m */,
727728
);
728729
name = "Branch-SDK-Tests";
729730
path = "../Branch-SDK-Tests";
@@ -763,7 +764,6 @@
763764
5F8B7B3C21B5F5CD009CE0A6 /* Branch-SDK-Unhosted-Tests */ = {
764765
isa = PBXGroup;
765766
children = (
766-
4A0C5F942397006800AE6628 /* BNCDelayedInitSessionTests.m */,
767767
5FC7327622DE9A44006E6FBC /* BNCServerInterfaceTests.m */,
768768
5F67F48D228F535500067429 /* BNCEncodingUtilsTests.m */,
769769
5F8B7B4621B5F5F0009CE0A6 /* Branch_setBranchKeyTests.m */,
@@ -1358,7 +1358,6 @@
13581358
files = (
13591359
5F67F48E228F535500067429 /* BNCEncodingUtilsTests.m in Sources */,
13601360
5F8B7B4721B5F5F0009CE0A6 /* Branch_setBranchKeyTests.m in Sources */,
1361-
4A0C5F982397176500AE6628 /* BNCDelayedInitSessionTests.m in Sources */,
13621361
5F3D671C233062FD00454FF1 /* BNCJsonLoader.m in Sources */,
13631362
5FC7327722DE9A44006E6FBC /* BNCServerInterfaceTests.m in Sources */,
13641363
);
@@ -1402,6 +1401,7 @@
14021401
4D1683B92098C902008819E3 /* BNCSystemObserver.Test.m in Sources */,
14031402
4D1683BE2098C902008819E3 /* BranchShortUrlSyncRequestTests.m in Sources */,
14041403
4D1683CA2098C902008819E3 /* BNCPreferenceHelperTests.m in Sources */,
1404+
4AB16368239E3A2700D42931 /* BNCPreInitBlockTests.m in Sources */,
14051405
4D1683B32098C902008819E3 /* BranchGetCreditHistoryRequestTests.m in Sources */,
14061406
5F892ECE23624E0A0023AEC1 /* BNCFacebookAppLinksTests.m in Sources */,
14071407
4D1683BB2098C902008819E3 /* BranchShortUrlRequestTests.m in Sources */,

0 commit comments

Comments
 (0)