Skip to content

Commit e5291c3

Browse files
authored
Queued Opens AIS-128 (#506)
* Updated preference helper. * Updated server queue. * Tested & fixed for iOS 10/9/8. * Updated storyboards for Xcode 7. * Added try/catch to NSKeyedArchiver call.
1 parent 16b65c9 commit e5291c3

File tree

7 files changed

+74
-66
lines changed

7 files changed

+74
-66
lines changed

Branch-SDK/Branch-SDK/BNCPreferenceHelper.m

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -374,17 +374,20 @@ - (void)setUniversalLinkUrl:(NSString *)universalLinkUrl {
374374
}
375375

376376
- (NSString *)sessionParams {
377-
if (_sessionParams) {
378-
_sessionParams = [self readStringFromDefaults:BRANCH_PREFS_KEY_SESSION_PARAMS];
377+
@synchronized (self) {
378+
if (!_sessionParams) {
379+
_sessionParams = [self readStringFromDefaults:BRANCH_PREFS_KEY_SESSION_PARAMS];
380+
}
381+
return _sessionParams;
379382
}
380-
381-
return _sessionParams;
382383
}
383384

384385
- (void)setSessionParams:(NSString *)sessionParams {
385-
if (![_sessionParams isEqualToString:sessionParams]) {
386-
_sessionParams = sessionParams;
387-
[self writeObjectToDefaults:BRANCH_PREFS_KEY_SESSION_PARAMS value:sessionParams];
386+
@synchronized (self) {
387+
if (![_sessionParams isEqualToString:sessionParams]) {
388+
_sessionParams = sessionParams;
389+
[self writeObjectToDefaults:BRANCH_PREFS_KEY_SESSION_PARAMS value:sessionParams];
390+
}
388391
}
389392
}
390393

@@ -606,42 +609,48 @@ - (NSDictionary *)getContentAnalyticsManifest {
606609
return (NSDictionary *)[self readObjectFromDefaults:BRANCH_PREFS_KEY_ANALYTICS_MANIFEST];
607610
}
608611

612+
609613
#pragma mark - Writing To Persistence
610614

615+
611616
- (void)writeIntegerToDefaults:(NSString *)key value:(NSInteger)value {
612-
self.persistenceDict[key] = @(value);
613-
[self persistPrefsToDisk];
617+
[self writeObjectToDefaults:key value:@(value)];
614618
}
615619

616620
- (void)writeBoolToDefaults:(NSString *)key value:(BOOL)value {
617-
self.persistenceDict[key] = @(value);
618-
[self persistPrefsToDisk];
621+
[self writeObjectToDefaults:key value:@(value)];
619622
}
620623

621624
- (void)writeObjectToDefaults:(NSString *)key value:(NSObject *)value {
622-
if (value) {
623-
self.persistenceDict[key] = value;
624-
}
625-
else {
626-
[self.persistenceDict removeObjectForKey:key];
625+
@synchronized (self) {
626+
if (value) {
627+
self.persistenceDict[key] = value;
628+
}
629+
else {
630+
[self.persistenceDict removeObjectForKey:key];
631+
}
632+
[self persistPrefsToDisk];
627633
}
628-
629-
[self persistPrefsToDisk];
630634
}
631635

632636
- (void)persistPrefsToDisk {
633637
@synchronized (self) {
634-
NSDictionary *persistenceDict = [self.persistenceDict copy];
638+
if (!self.persistenceDict) return;
639+
NSData *data = nil;
640+
@try {
641+
data = [NSKeyedArchiver archivedDataWithRootObject:self.persistenceDict];
642+
}
643+
@catch (id exception) {
644+
data = nil;
645+
[self logWarning:
646+
[NSString stringWithFormat:@"Exception creating preferences data: %@.",
647+
exception]];
648+
}
649+
if (!data) {
650+
[self logWarning:@"Can't create preferences data."];
651+
return;
652+
}
635653
NSBlockOperation *newPersistOp = [NSBlockOperation blockOperationWithBlock:^ {
636-
NSData *data = nil;
637-
@try {
638-
data = [NSKeyedArchiver archivedDataWithRootObject:persistenceDict];
639-
} @catch (id n) {
640-
}
641-
if (!data) {
642-
[self logWarning:@"Can't create preferences archive."];
643-
return;
644-
}
645654
NSError *error = nil;
646655
[data writeToURL:self.class.URLForPrefsFile
647656
options:NSDataWritingAtomic error:&error];
@@ -651,7 +660,7 @@ - (void)persistPrefsToDisk {
651660
@"Failed to persist preferences to disk: %@.", error]];
652661
}
653662
}];
654-
[self.persistPrefsQueue addOperation:newPersistOp];
663+
[self.persistPrefsQueue addOperation:newPersistOp];
655664
}
656665
}
657666

Branch-SDK/Branch-SDK/BNCServerRequestQueue.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- (void)clearQueue;
2727

2828
- (BOOL)containsInstallOrOpen;
29+
- (BOOL)removeInstallOrOpen;
2930
- (BOOL)containsClose;
3031
- (BranchOpenRequest *)moveInstallOrOpenToFront:(NSInteger)networkCount;
3132

Branch-SDK/Branch-SDK/BNCServerRequestQueue.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,21 @@ - (BOOL)containsInstallOrOpen {
131131
return NO;
132132
}
133133

134+
- (BOOL)removeInstallOrOpen {
135+
@synchronized (self.queue) {
136+
for (int i = 0; i < self.queue.count; i++) {
137+
BranchOpenRequest *req = [self.queue objectAtIndex:i];
138+
// Install extends open, so only need to check open.
139+
if ([req isKindOfClass:[BranchOpenRequest class]]) {
140+
req.callback = nil;
141+
[self remove:req];
142+
return YES;
143+
}
144+
}
145+
return NO;
146+
}
147+
}
148+
134149
- (BranchOpenRequest *)moveInstallOrOpenToFront:(NSInteger)networkCount {
135150
BOOL requestAlreadyInProgress = networkCount > 0;
136151

Branch-SDK/Branch-SDK/Branch.m

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,10 @@ @interface Branch() <BranchDeepLinkingControllerCompletionDelegate, FABKit>
7272

7373

7474
@property (strong, nonatomic) BNCServerInterface *bServerInterface;
75-
@property (strong, nonatomic) NSTimer *sessionTimer;
7675
@property (strong, nonatomic) BNCServerRequestQueue *requestQueue;
7776
@property (strong, nonatomic) dispatch_semaphore_t processing_sema;
78-
@property (strong, nonatomic) callbackWithParams sessionInitWithParamsCallback;
79-
@property (strong, nonatomic) callbackWithBranchUniversalObject sessionInitWithBranchUniversalObjectCallback;
77+
@property (copy, nonatomic) callbackWithParams sessionInitWithParamsCallback;
78+
@property (copy, nonatomic) callbackWithBranchUniversalObject sessionInitWithBranchUniversalObjectCallback;
8079
@property (assign, nonatomic) NSInteger networkCount;
8180
@property (assign, nonatomic) BOOL isInitialized;
8281
@property (assign, nonatomic) BOOL shouldCallSessionInitCallback;
@@ -86,15 +85,15 @@ @interface Branch() <BranchDeepLinkingControllerCompletionDelegate, FABKit>
8685
@property (strong, nonatomic) BNCContentDiscoveryManager *contentDiscoveryManager;
8786
@property (strong, nonatomic) NSString *branchKey;
8887
@property (strong, nonatomic) NSMutableDictionary *deepLinkControllers;
89-
@property (weak, nonatomic) UIViewController *deepLinkPresentingController;
88+
@property (weak, nonatomic) UIViewController *deepLinkPresentingController;
9089
@property (assign, nonatomic) BOOL useCookieBasedMatching;
9190
@property (strong, nonatomic) NSDictionary *deepLinkDebugParams;
9291
@property (assign, nonatomic) BOOL accountForFacebookSDK;
9392
@property (assign, nonatomic) id FBSDKAppLinkUtility;
9493
@property (assign, nonatomic) BOOL delayForAppleAds;
9594
@property (assign, nonatomic) BOOL searchAdsDebugMode;
9695
@property (strong, nonatomic) NSMutableArray *whiteListedSchemeList;
97-
96+
@property (assign, nonatomic) BOOL appIsInBackground;
9897
@end
9998

10099
@implementation Branch
@@ -1152,6 +1151,7 @@ - (BNCLinkData *)prepareLinkDataFor:(NSArray *)tags andAlias:(NSString *)alias a
11521151

11531152
#pragma mark - BranchUniversalObject methods
11541153

1154+
11551155
- (void)registerViewWithParams:(NSDictionary *)params andCallback:(callbackWithParams)callback {
11561156
[self initSessionIfNeededAndNotInProgress];
11571157

@@ -1164,22 +1164,18 @@ - (void)registerViewWithParams:(NSDictionary *)params andCallback:(callbackWithP
11641164
#pragma mark - Application State Change methods
11651165

11661166
- (void)applicationDidBecomeActive {
1167-
[self clearTimer];
1167+
self.appIsInBackground = NO;
11681168
if (!self.isInitialized && !self.preferenceHelper.shouldWaitForInit && ![self.requestQueue containsInstallOrOpen]) {
11691169
[self initUserSessionAndCallCallback:YES];
11701170
}
11711171
}
11721172

11731173
- (void)applicationWillResignActive {
1174-
[self clearTimer];
1175-
self.sessionTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(callClose) userInfo:nil repeats:NO];
1174+
self.appIsInBackground = YES;
1175+
[self callClose];
11761176
[self.requestQueue persistImmediately];
11771177
}
11781178

1179-
- (void)clearTimer {
1180-
[self.sessionTimer invalidate];
1181-
}
1182-
11831179
- (void)callClose {
11841180
if (self.isInitialized) {
11851181
self.isInitialized = NO;
@@ -1268,11 +1264,7 @@ - (void)processNextQueueItem {
12681264
[req processResponse:nil error:[NSError errorWithDomain:BNCErrorDomain code:BNCInitError userInfo:@{ NSLocalizedDescriptionKey: @"Branch User Session has not been initialized" }]];
12691265
return;
12701266
}
1271-
1272-
if (![req isKindOfClass:[BranchCloseRequest class]]) {
1273-
[self clearTimer];
1274-
}
1275-
1267+
12761268
[req makeRequest:self.bServerInterface key:self.branchKey callback:callback];
12771269
}
12781270
}
@@ -1338,26 +1330,17 @@ - (void)registerInstallOrOpen:(Class)clazz {
13381330
if ([BNCSystemObserver getOSVersion].integerValue >= 9 && self.useCookieBasedMatching) {
13391331
[[BNCStrongMatchHelper strongMatchHelper] createStrongMatchWithBranchKey:self.branchKey];
13401332
}
1341-
1342-
// If there isn't already an Open / Install request, add one to the queue
1343-
if (![self.requestQueue containsInstallOrOpen]) {
1344-
BranchOpenRequest *req = [[clazz alloc] initWithCallback:initSessionCallback];
1345-
1346-
[self insertRequestAtFront:req];
1347-
}
1348-
// If there is already one in the queue, make sure it's in the front.
1349-
// Make sure a callback is associated with this request. This callback can
1350-
// be cleared if the app is terminated while an Open/Install is pending.
1351-
else {
1352-
BranchOpenRequest *req = [self.requestQueue moveInstallOrOpenToFront:self.networkCount];
1353-
req.callback = initSessionCallback;
1354-
}
1355-
1333+
1334+
if ([self.requestQueue removeInstallOrOpen])
1335+
self.networkCount = 0;
1336+
BranchOpenRequest *req = [[clazz alloc] initWithCallback:initSessionCallback];
1337+
[self insertRequestAtFront:req];
13561338
[self processNextQueueItem];
13571339
}
13581340

13591341
- (void)handleInitSuccess {
1360-
self.isInitialized = YES;
1342+
if (!self.appIsInBackground)
1343+
self.isInitialized = YES;
13611344

13621345
NSDictionary *latestReferringParams = [self getLatestReferringParams];
13631346
if (self.shouldCallSessionInitCallback) {
@@ -1436,4 +1419,4 @@ + (NSString *)kitDisplayVersion {
14361419
return @"0.12.16";
14371420
}
14381421

1439-
@end
1422+
@end

Branch-SDK/Branch-SDK/Requests/BranchOpenRequest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@interface BranchOpenRequest : BNCServerRequest
1313

14-
@property (strong, nonatomic) callbackWithStatus callback;
14+
@property (copy, nonatomic) callbackWithStatus callback;
1515

1616
- (id)initWithCallback:(callbackWithStatus)callback;
1717
- (id)initWithCallback:(callbackWithStatus)callback isInstall:(BOOL)isInstall;

Branch-SDK/Branch-SDK/Requests/BranchOpenRequest.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ @interface BranchOpenRequest ()
2222

2323
@end
2424

25+
2526
@implementation BranchOpenRequest
2627

2728
- (id)initWithCallback:(callbackWithStatus)callback {

Branch-TestBed/Branch-TestBed/main.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
#import "AppDelegate.h"
1212

13-
int main(int argc, char * argv[])
14-
{
13+
int main(int argc, char * argv[]) {
1514
@autoreleasepool {
1615
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
1716
}

0 commit comments

Comments
 (0)