Skip to content

Commit eb958c0

Browse files
authored
fix(*): Make sure start/track, UI, callbacks are not run in background
In some occasion UI and callbacks was running in background. This commit prevents it. Also protects against misuse of start/track. JIRA: LP-5794
1 parent b58cbd9 commit eb958c0

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

Leanplum-SDK/Classes/LPEventCallbackManager.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ + (void)invokeSuccessCallbacksOnResponses:(id)responses
8484
requests:(NSArray *)requests
8585
operation:(id<LPNetworkOperationProtocol>)operation
8686
{
87+
// Ensure all callbacks are on main thread.
88+
if (![NSThread isMainThread]) {
89+
dispatch_sync(dispatch_get_main_queue(), ^{
90+
[self invokeSuccessCallbacksOnResponses:responses requests:requests operation:operation];
91+
});
92+
return;
93+
}
94+
8795
// Invoke and remove the callbacks that have errors.
8896
[LPEventCallbackManager invokeErrorCallbacksOnResponses:responses];
8997

@@ -117,6 +125,14 @@ + (void)invokeSuccessCallbacksOnResponses:(id)responses
117125

118126
+ (void)invokeErrorCallbacksOnResponses:(id)responses
119127
{
128+
// Ensure all callbacks are on main thread.
129+
if (![NSThread isMainThread]) {
130+
dispatch_sync(dispatch_get_main_queue(), ^{
131+
[self invokeErrorCallbacksOnResponses:responses];
132+
});
133+
return;
134+
}
135+
120136
// Handle errors that don't return an HTTP error code.
121137
NSMutableDictionary *callbackMap = [LPEventCallbackManager eventCallbackMap];
122138
for (NSUInteger i = 0; i < [LPResponse numResponsesInDictionary:responses]; i++) {

Leanplum-SDK/Classes/LPMessageTemplates.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,14 @@ + (UIImage *)dismissImage:(UIColor *)color withSize:(int)size
568568
// Displays the Center Popup, Interstitial and Web Interstitial.
569569
- (void)showPopup
570570
{
571+
// UI can't be modified in background.
572+
if (![NSThread isMainThread]) {
573+
dispatch_sync(dispatch_get_main_queue(), ^{
574+
[self showPopup];
575+
});
576+
return;
577+
}
578+
571579
LPActionContext *context = _contexts.lastObject;
572580
BOOL isFullscreen = [context.actionName isEqualToString:LPMT_INTERSTITIAL_NAME];
573581
BOOL isWeb = [context.actionName isEqualToString:LPMT_WEB_INTERSTITIAL_NAME] ||
@@ -717,6 +725,14 @@ - (void)closePopupWithAnimation:(BOOL)animated
717725
return;
718726
}
719727

728+
// UI can't be modified in background.
729+
if (![NSThread isMainThread]) {
730+
dispatch_sync(dispatch_get_main_queue(), ^{
731+
[self closePopupWithAnimation:animated actionNamed:actionName track:track];
732+
});
733+
return;
734+
}
735+
720736
LPActionContext *context = _contexts.lastObject;
721737
[_contexts removeLastObject];
722738

Leanplum-SDK/Classes/Leanplum.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,15 @@ + (void)startWithUserId:(NSString *)userId
725725
@"methods."];
726726
return;
727727
}
728+
729+
// Leanplum should not be started in background.
730+
if (![NSThread isMainThread]) {
731+
dispatch_sync(dispatch_get_main_queue(), ^{
732+
[self startWithUserId:userId userAttributes:attributes responseHandler:startResponse];
733+
});
734+
return;
735+
}
736+
728737
LP_TRY
729738
NSDate *startTime = [NSDate date];
730739
if (startResponse) {
@@ -1844,6 +1853,15 @@ + (void)track:(NSString *)event withValue:(double)value andInfo:(NSString *)info
18441853
{
18451854
RETURN_IF_NOOP;
18461855
LP_TRY
1856+
1857+
// Track should not be called in background.
1858+
if (![NSThread isMainThread]) {
1859+
dispatch_sync(dispatch_get_main_queue(), ^{
1860+
[self track:event withValue:value andInfo:info andArgs:args andParameters:params];
1861+
});
1862+
return;
1863+
}
1864+
18471865
NSString *valueStr = [NSString stringWithFormat:@"%f", value];
18481866
NSMutableDictionary *arguments = [NSMutableDictionary dictionaryWithObjectsAndKeys:
18491867
valueStr, LP_PARAM_VALUE, nil];

0 commit comments

Comments
 (0)