Skip to content

Commit b9c15b4

Browse files
author
Alexis Oyama
committed
fix(event): Pause event was sent without going into operation queue
1 parent be6565a commit b9c15b4

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed

Leanplum-SDK/Classes/Leanplum.m

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,42 +1148,29 @@ + (void)maybeRegisterForNotifications
11481148
#endif
11491149
}
11501150

1151-
/**
1152-
* Use this to make sure the task fully runs when the app is in background.
1153-
* Mainly used by {@link pause}.
1154-
*/
1155-
+ (void)beginBackgroundTask:(void(^)())task expirationHandler:(void(^)())expirationHandler
1151+
+ (void)pause
11561152
{
11571153
UIApplication *application = [UIApplication sharedApplication];
11581154
UIBackgroundTaskIdentifier __block backgroundTask;
1159-
1160-
// Start background task.
1161-
backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{
1162-
if (expirationHandler) {
1163-
expirationHandler();
1164-
}
1165-
1155+
1156+
// Block that finish task.
1157+
void (^finishTaskHandler)() = ^(){
11661158
[application endBackgroundTask:backgroundTask];
11671159
backgroundTask = UIBackgroundTaskInvalid;
1160+
};
1161+
1162+
// Start background task to make sure it runs when the app is in background.
1163+
backgroundTask = [application beginBackgroundTaskWithExpirationHandler:finishTaskHandler];
1164+
1165+
// Send pause event.
1166+
LeanplumRequest *request = [LeanplumRequest post:LP_METHOD_PAUSE_SESSION params:nil];
1167+
[request onResponse:^(id<LPNetworkOperationProtocol> operation, id json) {
1168+
finishTaskHandler();
11681169
}];
1169-
1170-
// Start the long-running task and return immediately.
1171-
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
1172-
if (task) {
1173-
task();
1174-
}
1175-
1176-
[application endBackgroundTask:backgroundTask];
1177-
backgroundTask = UIBackgroundTaskInvalid;
1178-
});
1179-
}
1180-
1181-
+ (void)pause
1182-
{
1183-
[self beginBackgroundTask:^{
1184-
[[LeanplumRequest post:LP_METHOD_PAUSE_SESSION params:nil] sendIfConnectedSync:YES];
1185-
[[NSUserDefaults standardUserDefaults] synchronize];
1186-
} expirationHandler:nil];
1170+
[request onError:^(NSError *error) {
1171+
finishTaskHandler();
1172+
}];
1173+
[request sendIfConnected];
11871174
}
11881175

11891176
+ (void)resume

0 commit comments

Comments
 (0)