Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK-2309 cache link data when initialization is deferred #1369

Merged
merged 5 commits into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions Sources/BranchSDK/Branch.m
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
// This is enabled by setting deferInitForPluginRuntime to true in branch.json
@property (nonatomic, assign, readwrite) BOOL deferInitForPluginRuntime;
@property (nonatomic, copy, nullable) void (^cachedInitBlock)(void);
@property (nonatomic, copy, readwrite) NSString *cachedURLString;

@end

Expand Down Expand Up @@ -624,27 +625,20 @@
[self.class addBranchSDKVersionToCrashlyticsReport];
self.shouldAutomaticallyDeepLink = automaticallyDisplayController;

// If the SDK is already initialized, this means that initSession was called after other lifecycle calls.
if (self.initializationStatus == BNCInitStatusInitialized) {
[self initUserSessionAndCallCallback:YES sceneIdentifier:nil urlString:nil];
return;
}

// Save data from push notification on app launch
// Check for Branch link in a push payload
NSString *pushURL = nil;
#if !TARGET_OS_TV
if ([options objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
id branchUrlFromPush = [options objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey][BRANCH_PUSH_NOTIFICATION_PAYLOAD_KEY];
if ([branchUrlFromPush isKindOfClass:[NSString class]]) {
self.preferenceHelper.universalLinkUrl = branchUrlFromPush;
self.preferenceHelper.referringURL = branchUrlFromPush;
pushURL = (NSString *)branchUrlFromPush;

Check warning on line 636 in Sources/BranchSDK/Branch.m

View check run for this annotation

Codecov / codecov/patch

Sources/BranchSDK/Branch.m#L636

Added line #L636 was not covered by tests
}
}
#endif

// Handle case where there's no URI scheme or Universal Link.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code assumed that there would be a follow up link lifecycle call, but with delayed init that call may have already happened and the associated link saved off.

Currently researching if this change has side effects.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some testing this appears to be a legacy check that hasn't been required in a long time. It did negatively impact apps are are using just an AppDelegate on cold launch with a delayed init. Removing the check fixes the issue.

if (![options.allKeys containsObject:UIApplicationLaunchOptionsURLKey] && ![options.allKeys containsObject:UIApplicationLaunchOptionsUserActivityDictionaryKey]) {
[self initUserSessionAndCallCallback:YES sceneIdentifier:nil urlString:nil];
}
[self initUserSessionAndCallCallback:YES sceneIdentifier:nil urlString:pushURL];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment if there are any assumptions about from which lifecycle call(s) this must go through?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously we assumed that initSession always came first, and other lifecycle calls would follow. With the delay feature, we cannot assume that anymore and cache away links arriving from other lifecycle calls. So now there are no assumptions about where the initSession is coming from.

}

- (void)setDeepLinkDebugMode:(NSDictionary *)debugParams {
Expand Down Expand Up @@ -1938,11 +1932,21 @@

- (void)initUserSessionAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSString *)sceneIdentifier urlString:(NSString *)urlString {

// ignore lifecycle calls while waiting for a plugin runtime.
@synchronized (self) {
if (self.deferInitForPluginRuntime) {
[[BranchLogger shared] logDebug:@"Branch init is deferred, ignoring init call." error:nil];
if (urlString) {
echo-branch marked this conversation as resolved.
Show resolved Hide resolved
[[BranchLogger shared] logDebug:@"Branch init is deferred, caching link" error:nil];
self.cachedURLString = urlString;

Check warning on line 1939 in Sources/BranchSDK/Branch.m

View check run for this annotation

Codecov / codecov/patch

Sources/BranchSDK/Branch.m#L1938-L1939

Added lines #L1938 - L1939 were not covered by tests
} else {
[[BranchLogger shared] logDebug:@"Branch init is deferred, ignoring lifecycle call without a link" error:nil];

Check warning on line 1941 in Sources/BranchSDK/Branch.m

View check run for this annotation

Codecov / codecov/patch

Sources/BranchSDK/Branch.m#L1941

Added line #L1941 was not covered by tests
}
return;
} else {
if (!urlString && self.cachedURLString) {
urlString = self.cachedURLString;
[[BranchLogger shared] logDebug:[NSString stringWithFormat:@"Using cached link: %@", urlString] error:nil];

Check warning on line 1947 in Sources/BranchSDK/Branch.m

View check run for this annotation

Codecov / codecov/patch

Sources/BranchSDK/Branch.m#L1946-L1947

Added lines #L1946 - L1947 were not covered by tests
}
self.cachedURLString = nil;
}
}

Expand Down
Loading