Skip to content

Commit aae4eed

Browse files
committed
fix(ios): avoid reusing background completion handlers
1 parent 962e7a4 commit aae4eed

3 files changed

Lines changed: 8 additions & 11 deletions

File tree

iphone/Classes/TiAppiOSProxy.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ - (void)endBackgroundHandler:(id)handlerIdentifier
977977
if ([handlerIdentifier rangeOfString:@"Session"].location != NSNotFound) {
978978
[[TiApp app] performCompletionHandlerForBackgroundTransferWithKey:handlerIdentifier];
979979
} else {
980-
[[TiApp app] performCompletionHandlerWithKey:handlerIdentifier andResult:UIBackgroundFetchResultNoData removeAfterExecution:NO];
980+
[[TiApp app] performCompletionHandlerWithKey:handlerIdentifier andResult:UIBackgroundFetchResultNoData];
981981
}
982982
}
983983

iphone/TitaniumKit/TitaniumKit/Sources/API/TiApp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ TI_INLINE void waitForMemoryPanicCleared() // WARNING: This must never be run on
297297
- (void)registerBackgroundService:(TiProxy *)proxy;
298298
- (void)unregisterBackgroundService:(TiProxy *)proxy;
299299
- (void)stopBackgroundService:(TiProxy *)proxy;
300-
- (void)performCompletionHandlerWithKey:(NSString *)key andResult:(UIBackgroundFetchResult)result removeAfterExecution:(BOOL)removeAfterExecution;
300+
- (void)performCompletionHandlerWithKey:(NSString *)key andResult:(UIBackgroundFetchResult)result;
301301
- (void)performCompletionHandlerForBackgroundTransferWithKey:(NSString *)key;
302302
- (void)watchKitExtensionRequestHandler:(id)key withUserInfo:(NSDictionary *)userInfo;
303303

iphone/TitaniumKit/TitaniumKit/Sources/API/TiApp.m

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,7 @@ - (void)flushCompletionHandlerQueue
759759
{
760760
if (pendingCompletionHandlers != nil) {
761761
for (NSString *key in [pendingCompletionHandlers allKeys]) {
762-
// Do not remove from the pending handlers for now, as it's removed after the enumeration finished
763-
[self performCompletionHandlerWithKey:key andResult:UIBackgroundFetchResultFailed removeAfterExecution:NO];
762+
[self performCompletionHandlerWithKey:key andResult:UIBackgroundFetchResultFailed];
764763
}
765764
}
766765
RELEASE_TO_NIL(pendingCompletionHandlers);
@@ -772,19 +771,17 @@ - (void)fireCompletionHandler:(NSTimer *)timer
772771
NSString *key = (NSString *)timer.userInfo;
773772
if ([pendingCompletionHandlers objectForKey:key]) {
774773
// Send an event notifying the developer that the background-fetch failed
775-
[self performCompletionHandlerWithKey:key andResult:UIBackgroundFetchResultFailed removeAfterExecution:YES];
774+
[self performCompletionHandlerWithKey:key andResult:UIBackgroundFetchResultFailed];
776775
}
777776
}
778777

779778
// Gets called when user ends finishes with backgrounding stuff. By default this would always be called with UIBackgroundFetchResultNoData.
780-
- (void)performCompletionHandlerWithKey:(NSString *)key andResult:(UIBackgroundFetchResult)result removeAfterExecution:(BOOL)removeAfterExecution
779+
- (void)performCompletionHandlerWithKey:(NSString *)key andResult:(UIBackgroundFetchResult)result
781780
{
782-
if ([pendingCompletionHandlers objectForKey:key]) {
783-
void (^completionHandler)(UIBackgroundFetchResult) = [pendingCompletionHandlers objectForKey:key];
781+
void (^completionHandler)(UIBackgroundFetchResult) = [pendingCompletionHandlers objectForKey:key];
782+
if (completionHandler != nil) {
783+
[pendingCompletionHandlers removeObjectForKey:key];
784784
completionHandler(result);
785-
if (removeAfterExecution) {
786-
[pendingCompletionHandlers removeObjectForKey:key];
787-
}
788785
} else {
789786
DebugLog(@"[ERROR] The specified completion handler with ID = %@ has already expired or been removed from the system", key);
790787
}

0 commit comments

Comments
 (0)