Skip to content

Commit 4d166ca

Browse files
committed
fix(messaging, ios): serialize access to background handler state
this should make race conditions impossible, in case that was the cause for a recent crash note that the 25 second timeout block did *not* execute in testing if the completion handler was called - the app went back to sleep immediately, so no create_dispatch_block / dispatch_block_cancel was needed
1 parent 153637d commit 4d166ca

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

packages/messaging/ios/RNFBMessaging/RNFBMessaging+AppDelegate.m

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,16 @@ - (void)application:(UIApplication *)application
163163
// If app is in background state, register background task to guarantee async queues aren't
164164
// frozen.
165165
sharedInstance.backgroundTaskId = [application beginBackgroundTaskWithExpirationHandler:^{
166-
if (sharedInstance.backgroundTaskId != UIBackgroundTaskInvalid) {
167-
[application endBackgroundTask:sharedInstance.backgroundTaskId];
168-
sharedInstance.backgroundTaskId = UIBackgroundTaskInvalid;
169-
}
166+
dispatch_get_main_queue(), ^{
167+
if (sharedInstance.backgroundTaskId != UIBackgroundTaskInvalid) {
168+
[application endBackgroundTask:sharedInstance.backgroundTaskId];
169+
sharedInstance.backgroundTaskId = UIBackgroundTaskInvalid;
170+
}
171+
};
170172
}];
171173

172174
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(25 * NSEC_PER_SEC)),
173-
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
175+
dispatch_get_main_queue(), ^{
174176
if (sharedInstance.completionHandler) {
175177
sharedInstance.completionHandler(UIBackgroundFetchResultNewData);
176178
sharedInstance.completionHandler = nil;

packages/messaging/ios/RNFBMessaging/RNFBMessagingModule.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ - (NSDictionary *)constantsToExport {
220220
}
221221

222222
RCT_EXPORT_METHOD(completeNotificationProcessing) {
223-
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
223+
dispatch_get_main_queue(), ^{
224224
RNFBMessagingAppDelegate *appDelegate = [RNFBMessagingAppDelegate sharedInstance];
225225
if (appDelegate.completionHandler) {
226226
appDelegate.completionHandler(UIBackgroundFetchResultNewData);
@@ -230,7 +230,7 @@ - (NSDictionary *)constantsToExport {
230230
[[UIApplication sharedApplication] endBackgroundTask:appDelegate.backgroundTaskId];
231231
appDelegate.backgroundTaskId = UIBackgroundTaskInvalid;
232232
}
233-
});
233+
};
234234
}
235235

236236
RCT_EXPORT_METHOD(requestPermission

0 commit comments

Comments
 (0)