Skip to content

Commit c13149f

Browse files
committed
SDK-777 save work in progress
1 parent 7d52a81 commit c13149f

File tree

7 files changed

+210
-3
lines changed

7 files changed

+210
-3
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//
2+
// BNCCallbackMapTests.m
3+
// Branch-SDK-Tests
4+
//
5+
// Created by Ernest Cho on 2/25/20.
6+
// Copyright © 2020 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import "BNCCallbackMap.h"
11+
12+
// expose private storage object for state checks
13+
@interface BNCCallbackMap()
14+
@property (nonatomic, strong, readwrite) NSMapTable *callbacks;
15+
@end
16+
17+
@interface BNCCallbackMapTests : XCTestCase
18+
19+
@end
20+
21+
@implementation BNCCallbackMapTests
22+
23+
- (void)setUp {
24+
// Put setup code here. This method is called before the invocation of each test method in the class.
25+
}
26+
27+
- (void)tearDown {
28+
// Put teardown code here. This method is called after the invocation of each test method in the class.
29+
}
30+
31+
- (void)testSingleSave {
32+
BNCCallbackMap *map = [BNCCallbackMap new];
33+
34+
// block variable callback will update
35+
__block NSString *status = @"no callback";
36+
37+
// store a request and callback block
38+
BNCServerRequest *request = [BNCServerRequest new];
39+
[map storeRequest:request withCompletion:^(NSString * _Nonnull statusMessage) {
40+
status = statusMessage;
41+
}];
42+
43+
// confirm there's one entry
44+
XCTAssert(map.callbacks.count == 1);
45+
46+
// call callback
47+
[map callCompletionForRequest:request withStatusMessage:@"callback"];
48+
49+
// check if variable was updated
50+
XCTAssert([@"callback" isEqualToString:status]);
51+
}
52+
53+
- (void)testDeletedRequest {
54+
BNCCallbackMap *map = [BNCCallbackMap new];
55+
56+
// block variable callback will update
57+
__block NSString *status = @"no callback";
58+
59+
// store a request and callback block
60+
BNCServerRequest *request = [BNCServerRequest new];
61+
[map storeRequest:request withCompletion:^(NSString * _Nonnull statusMessage) {
62+
status = statusMessage;
63+
}];
64+
65+
// confirm there's one entry
66+
XCTAssert(map.callbacks.count == 1);
67+
68+
// wipe the data from the map
69+
request = [BNCServerRequest new];
70+
71+
// call callback
72+
[map callCompletionForRequest:request withStatusMessage:@"callback"];
73+
74+
// check if variable was updated
75+
XCTAssert([@"no callback" isEqualToString:status]);
76+
}
77+
78+
- (void)testSeveralBlocks {
79+
BNCCallbackMap *map = [BNCCallbackMap new];
80+
81+
__block int count = 0;
82+
83+
BNCServerRequest *request = [BNCServerRequest new];
84+
[map storeRequest:request withCompletion:^(NSString * _Nonnull statusMessage) {
85+
count++;
86+
}];
87+
88+
for (int i=0; i<100; i++) {
89+
BNCServerRequest *tmp = [BNCServerRequest new];
90+
[map storeRequest:tmp withCompletion:^(NSString * _Nonnull statusMessage) {
91+
count++;
92+
}];
93+
}
94+
95+
// confirm there's less than 100 entries. By not retaining the tmp request, they should be getting ARC'd
96+
XCTAssert(map.callbacks.count < 100);
97+
98+
[map callCompletionForRequest:request withStatusMessage:@"callback"];
99+
XCTAssert(count == 1);
100+
}
101+
102+
@end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// BNCCallbackMap.h
3+
// Branch
4+
//
5+
// Created by Ernest Cho on 2/25/20.
6+
// Copyright © 2020 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "BNCServerRequest.h"
11+
12+
NS_ASSUME_NONNULL_BEGIN
13+
14+
@interface BNCCallbackMap : NSObject
15+
16+
+ (instancetype)shared;
17+
18+
- (void)storeRequest:(BNCServerRequest *)request withCompletion:(void (^_Nullable)(NSString *statusMessage))completion;
19+
20+
- (void)callCompletionForRequest:(BNCServerRequest *)request withStatusMessage:(NSString *)statusMessage;
21+
22+
@end
23+
24+
NS_ASSUME_NONNULL_END
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// BNCCallbackMap.m
3+
// Branch
4+
//
5+
// Created by Ernest Cho on 2/25/20.
6+
// Copyright © 2020 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import "BNCCallbackMap.h"
10+
11+
@interface BNCCallbackMap()
12+
@property (nonatomic, strong, readwrite) NSMapTable *callbacks;
13+
@end
14+
15+
@implementation BNCCallbackMap
16+
17+
+ (instancetype)shared {
18+
static BNCCallbackMap *map;
19+
static dispatch_once_t onceToken;
20+
dispatch_once(&onceToken, ^{
21+
map = [BNCCallbackMap new];
22+
});
23+
return map;
24+
}
25+
26+
- (instancetype)init {
27+
self = [super init];
28+
if (self) {
29+
30+
// the key is a weak pointer to the request object
31+
// the value is a strong pointer to the request callback block
32+
// if the request object becomes nil, the callback block is lost
33+
self.callbacks = [NSMapTable mapTableWithKeyOptions:NSMapTableWeakMemory valueOptions:NSMapTableStrongMemory];
34+
}
35+
return self;
36+
}
37+
38+
- (void)storeRequest:(BNCServerRequest *)request withCompletion:(void (^_Nullable)(NSString *statusMessage))completion {
39+
[self.callbacks setObject:completion forKey:request];
40+
}
41+
42+
- (void)callCompletionForRequest:(BNCServerRequest *)request withStatusMessage:(NSString *)statusMessage {
43+
void (^completion)(NSString *) = [self.callbacks objectForKey:request];
44+
if (completion) {
45+
completion(statusMessage);
46+
}
47+
}
48+
49+
@end

Branch-SDK/Branch-SDK/Branch.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#import "BNCAppleSearchAds.h"
4444
#import "BNCFacebookAppLinks.h"
4545
#import "BNCDeviceInfo.h"
46+
#import "BNCCallbackMap.h"
4647

4748
NSString * const BRANCH_FEATURE_TAG_SHARE = @"share";
4849
NSString * const BRANCH_FEATURE_TAG_REFERRAL = @"referral";

Branch-SDK/Branch-SDK/BranchEvent.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ typedef NS_ENUM(NSInteger, BranchEventAdType) {
8888
@property (nonatomic, copy) NSArray<BranchUniversalObject*>*_Nonnull contentItems;
8989
@property (nonatomic, copy) NSDictionary<NSString*, NSString*> *_Nonnull customData;
9090

91+
// test callback for branch event
92+
- (void)logEventWithCompletion:(void (^_Nullable)(NSString *statusMessage))completion;
93+
9194
- (void) logEvent; //!< Logs the event on the Branch server.
9295
- (NSDictionary*_Nonnull) dictionary; //!< Returns a dictionary representation of the event.
9396
- (NSString* _Nonnull) description; //!< Returns a string description of the event.

Branch-SDK/Branch-SDK/BranchEvent.m

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import "BranchEvent.h"
1010
#import "BNCLog.h"
11+
#import "BNCCallbackMap.h"
1112

1213
#pragma mark BranchStandardEvents
1314

@@ -179,7 +180,7 @@ - (NSDictionary*) dictionary {
179180
addString(coupon, coupon);
180181
addString(affiliation, affiliation);
181182
addString(eventDescription, description);
182-
addString(searchQuery, search_query)
183+
addString(searchQuery, search_query);
183184
addDictionary(customData, custom_data);
184185

185186
#include "BNCFieldDefines.h"
@@ -220,18 +221,29 @@ - (NSDictionary*) dictionary {
220221
];
221222
}
222223

223-
- (void) logEvent {
224-
224+
- (void)logEventWithCompletion:(void (^_Nullable)(NSString *statusMessage))completion {
225225
if (![_eventName isKindOfClass:[NSString class]] || _eventName.length == 0) {
226226
BNCLogError(@"Invalid event type '%@' or empty string.", NSStringFromClass(_eventName.class));
227+
228+
if (completion) {
229+
completion(@"Error");
230+
}
231+
227232
return;
228233
}
229234

230235
NSDictionary *eventDictionary = [self buildEventDictionary];
231236
BranchEventRequest *request = [self buildRequestWithEventDictionary:eventDictionary];
237+
238+
[[BNCCallbackMap shared] storeRequest:request withCompletion:completion];
239+
232240
[[Branch getInstance] sendServerRequest:request];
233241
}
234242

243+
- (void) logEvent {
244+
[self logEventWithCompletion:nil];
245+
}
246+
235247
- (BranchEventRequest *)buildRequestWithEventDictionary:(NSDictionary *)eventDictionary {
236248
BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper preferenceHelper];
237249

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@
162162
5F205D05231864E800C776D1 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5F205D04231864E800C776D1 /* WebKit.framework */; };
163163
5F205D062318659500C776D1 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5F205D04231864E800C776D1 /* WebKit.framework */; };
164164
5F205D0823186AF700C776D1 /* BNCUserAgentCollectorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F205D022318641700C776D1 /* BNCUserAgentCollectorTests.m */; };
165+
5F22AFC0240600A200837CF5 /* BNCCallbackMap.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FE693F62405E91500E3AEE2 /* BNCCallbackMap.m */; };
166+
5F22AFC1240600BE00837CF5 /* BNCCallbackMap.m in Headers */ = {isa = PBXBuildFile; fileRef = 5FE693F62405E91500E3AEE2 /* BNCCallbackMap.m */; };
165167
5F3D6714232C589100454FF1 /* BranchLastAttributedTouchData.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F3D6712232C589100454FF1 /* BranchLastAttributedTouchData.m */; };
166168
5F3D6715232C589100454FF1 /* BranchLastAttributedTouchData.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F3D6713232C589100454FF1 /* BranchLastAttributedTouchData.h */; settings = {ATTRIBUTES = (Public, ); }; };
167169
5F3D6718233061CB00454FF1 /* BranchCrossPlatformIDTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F3D6717233061CB00454FF1 /* BranchCrossPlatformIDTests.m */; };
@@ -230,6 +232,9 @@
230232
5FC7326922D81002006E6FBC /* BNCAppleReceipt.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FC7326722D81002006E6FBC /* BNCAppleReceipt.m */; };
231233
5FC7327022DD1F93006E6FBC /* BNCAppleReceiptTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FC7326F22DD1F93006E6FBC /* BNCAppleReceiptTests.m */; };
232234
5FC7327722DE9A44006E6FBC /* BNCServerInterfaceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FC7327622DE9A44006E6FBC /* BNCServerInterfaceTests.m */; };
235+
5FE693F72405E91500E3AEE2 /* BNCCallbackMap.h in Headers */ = {isa = PBXBuildFile; fileRef = 5FE693F52405E91500E3AEE2 /* BNCCallbackMap.h */; };
236+
5FE693F82405E91500E3AEE2 /* BNCCallbackMap.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FE693F62405E91500E3AEE2 /* BNCCallbackMap.m */; };
237+
5FE694382405FA2700E3AEE2 /* BNCCallbackMapTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FE694372405FA2700E3AEE2 /* BNCCallbackMapTests.m */; };
233238
63E4C4881D25E16A00A45FD8 /* LogOutputViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E4C4871D25E16A00A45FD8 /* LogOutputViewController.m */; };
234239
63E4C48B1D25E17B00A45FD8 /* NavigationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E4C48A1D25E17B00A45FD8 /* NavigationController.m */; };
235240
63E4C4901D25E1BC00A45FD8 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 63E4C48F1D25E1BC00A45FD8 /* LaunchScreen.storyboard */; };
@@ -517,6 +522,9 @@
517522
5FC7326722D81002006E6FBC /* BNCAppleReceipt.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BNCAppleReceipt.m; sourceTree = "<group>"; };
518523
5FC7326F22DD1F93006E6FBC /* BNCAppleReceiptTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BNCAppleReceiptTests.m; sourceTree = "<group>"; };
519524
5FC7327622DE9A44006E6FBC /* BNCServerInterfaceTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BNCServerInterfaceTests.m; sourceTree = "<group>"; };
525+
5FE693F52405E91500E3AEE2 /* BNCCallbackMap.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BNCCallbackMap.h; sourceTree = "<group>"; };
526+
5FE693F62405E91500E3AEE2 /* BNCCallbackMap.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BNCCallbackMap.m; sourceTree = "<group>"; };
527+
5FE694372405FA2700E3AEE2 /* BNCCallbackMapTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BNCCallbackMapTests.m; sourceTree = "<group>"; };
520528
63E4C4861D25E16A00A45FD8 /* LogOutputViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LogOutputViewController.h; sourceTree = "<group>"; };
521529
63E4C4871D25E16A00A45FD8 /* LogOutputViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LogOutputViewController.m; sourceTree = "<group>"; };
522530
63E4C4891D25E17B00A45FD8 /* NavigationController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NavigationController.h; sourceTree = "<group>"; };
@@ -763,6 +771,7 @@
763771
4AB16367239E3A2700D42931 /* DispatchToIsolationQueueTests.m */,
764772
5F437E39237DE3480052064B /* BNCTelephonyTests.m */,
765773
5F437E3F237E1A560052064B /* BNCDeviceSystemTests.m */,
774+
5FE694372405FA2700E3AEE2 /* BNCCallbackMapTests.m */,
766775
);
767776
name = "Branch-SDK-Tests";
768777
path = "../Branch-SDK-Tests";
@@ -997,6 +1006,8 @@
9971006
4D9607F31FBF9473008AB3C2 /* UIViewController+Branch.m */,
9981007
5F08460F23480008005B17E6 /* BNCTuneUtility.h */,
9991008
5F08461023480008005B17E6 /* BNCTuneUtility.m */,
1009+
5FE693F52405E91500E3AEE2 /* BNCCallbackMap.h */,
1010+
5FE693F62405E91500E3AEE2 /* BNCCallbackMap.m */,
10001011
);
10011012
name = "Branch-SDK";
10021013
path = "../Branch-SDK/Branch-SDK";
@@ -1049,6 +1060,7 @@
10491060
4DCAC8171F426F7C00405D1D /* BranchDeepLinkingController.h in Headers */,
10501061
5F892EB92360EFE90023AEC1 /* BNCAppleSearchAds.h in Headers */,
10511062
4DB327FF1FA10B9000ACF9B0 /* BranchDelegate.h in Headers */,
1063+
5F22AFC1240600BE00837CF5 /* BNCCallbackMap.m in Headers */,
10521064
4DCF4B031F438A8700AF9AAB /* BranchEvent.h in Headers */,
10531065
5F8F3E62232AD7F3002D0418 /* BranchCrossPlatformID.h in Headers */,
10541066
4DCAC8181F426F7C00405D1D /* BranchLinkProperties.h in Headers */,
@@ -1066,6 +1078,7 @@
10661078
5F92B23123834AFD00CA909B /* BNCReachability.h in Headers */,
10671079
5F909B6F2332B82200A774D2 /* BranchLATDRequest.h in Headers */,
10681080
4DCAC80E1F426F7C00405D1D /* BNCSystemObserver.h in Headers */,
1081+
5FE693F72405E91500E3AEE2 /* BNCCallbackMap.h in Headers */,
10691082
5F92B2392383703700CA909B /* BNCLocale.h in Headers */,
10701083
4DCAC8131F426F7C00405D1D /* BranchContentDiscoverer.h in Headers */,
10711084
4DCAC8141F426F7C00405D1D /* BranchContentDiscoveryManifest.h in Headers */,
@@ -1395,6 +1408,7 @@
13951408
4D7881F7209CF28F002B750F /* BNCThreads.m in Sources */,
13961409
5F892EBE2361157E0023AEC1 /* NSError+Branch.m in Sources */,
13971410
9A2B7DD61FEC3BAF00CD188B /* Branch+Validator.m in Sources */,
1411+
5FE693F82405E91500E3AEE2 /* BNCCallbackMap.m in Sources */,
13981412
4D130E921EE0C7B100A69A0A /* BranchShortUrlSyncRequest.m in Sources */,
13991413
5F92B240238486E200CA909B /* BNCNetworkInterface.m in Sources */,
14001414
466B586C1B17779C00A69EDE /* BranchActivityItemProvider.m in Sources */,
@@ -1441,6 +1455,7 @@
14411455
isa = PBXSourcesBuildPhase;
14421456
buildActionMask = 2147483647;
14431457
files = (
1458+
5F22AFC0240600A200837CF5 /* BNCCallbackMap.m in Sources */,
14441459
4D1683B72098C902008819E3 /* BNCTestCase.m in Sources */,
14451460
4D1683B82098C902008819E3 /* BNCEncodingUtils.Test.m in Sources */,
14461461
4D1683A72098C902008819E3 /* BranchCloseRequestTests.m in Sources */,
@@ -1466,6 +1481,7 @@
14661481
4AB16368239E3A2700D42931 /* DispatchToIsolationQueueTests.m in Sources */,
14671482
4D1683B32098C902008819E3 /* BranchGetCreditHistoryRequestTests.m in Sources */,
14681483
5F892ECE23624E0A0023AEC1 /* BNCFacebookAppLinksTests.m in Sources */,
1484+
5FE694382405FA2700E3AEE2 /* BNCCallbackMapTests.m in Sources */,
14691485
4D1683BB2098C902008819E3 /* BranchShortUrlRequestTests.m in Sources */,
14701486
4D1683C32098C902008819E3 /* BranchLoadRewardsRequestTests.m in Sources */,
14711487
4D1683BA2098C902008819E3 /* BNCLog.Test.m in Sources */,

0 commit comments

Comments
 (0)