Improve thread safety in PushManager and NotificationsManager.

// FREEBIE
pull/1/head
Matthew Chen 7 years ago
parent dca3fa5001
commit 3e53605009

@ -211,31 +211,33 @@
- (void)presentNotification:(UILocalNotification *)notification identifier:(NSString *)identifier
{
AssertIsOnMainThread();
// Replace any existing notification
// e.g. when an "Incoming Call" notification gets replaced with a "Missed Call" notification.
if (self.currentNotifications[identifier]) {
[self cancelNotificationWithIdentifier:identifier];
}
dispatch_async(dispatch_get_main_queue(), ^{
// Replace any existing notification
// e.g. when an "Incoming Call" notification gets replaced with a "Missed Call" notification.
if (self.currentNotifications[identifier]) {
[self cancelNotificationWithIdentifier:identifier];
}
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
DDLogDebug(@"%@ presenting notification with identifier: %@", self.tag, identifier);
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
DDLogDebug(@"%@ presenting notification with identifier: %@", self.tag, identifier);
self.currentNotifications[identifier] = notification;
self.currentNotifications[identifier] = notification;
});
}
- (void)cancelNotificationWithIdentifier:(NSString *)identifier
{
AssertIsOnMainThread();
UILocalNotification *notification = self.currentNotifications[identifier];
if (!notification) {
DDLogWarn(@"%@ Couldn't cancel notification because none was found with identifier: %@", self.tag, identifier);
return;
}
[self.currentNotifications removeObjectForKey:identifier];
dispatch_async(dispatch_get_main_queue(), ^{
UILocalNotification *notification = self.currentNotifications[identifier];
if (!notification) {
DDLogWarn(
@"%@ Couldn't cancel notification because none was found with identifier: %@", self.tag, identifier);
return;
}
[self.currentNotifications removeObjectForKey:identifier];
[[UIApplication sharedApplication] cancelLocalNotification:notification];
[[UIApplication sharedApplication] cancelLocalNotification:notification];
});
}
#pragma mark - Logging

@ -431,32 +431,34 @@ NSString *const PushManagerUserInfoKeysCallBackSignalRecipientId = @"PushManager
- (void)presentNotification:(UILocalNotification *)notification checkForCancel:(BOOL)checkForCancel
{
OWSAssert([NSThread isMainThread]);
NSString *threadId = notification.userInfo[Signal_Thread_UserInfo_Key];
if (checkForCancel && threadId != nil) {
// The longer we wait, the more obsolete notifications we can suppress -
// but the more lag we introduce to notification delivery.
const CGFloat kDelaySeconds = 0.3f;
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:kDelaySeconds];
notification.timeZone = [NSTimeZone localTimeZone];
}
dispatch_async(dispatch_get_main_queue(), ^{
NSString *threadId = notification.userInfo[Signal_Thread_UserInfo_Key];
if (checkForCancel && threadId != nil) {
// The longer we wait, the more obsolete notifications we can suppress -
// but the more lag we introduce to notification delivery.
const CGFloat kDelaySeconds = 0.3f;
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:kDelaySeconds];
notification.timeZone = [NSTimeZone localTimeZone];
}
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[self.currentNotifications addObject:notification];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[self.currentNotifications addObject:notification];
});
}
- (void)cancelNotificationsWithThreadId:(NSString *)threadId {
OWSAssert([NSThread isMainThread]);
NSMutableArray *toDelete = [NSMutableArray array];
[self.currentNotifications enumerateObjectsUsingBlock:^(UILocalNotification *notif, NSUInteger idx, BOOL *stop) {
if ([notif.userInfo[Signal_Thread_UserInfo_Key] isEqualToString:threadId]) {
[[UIApplication sharedApplication] cancelLocalNotification:notif];
[toDelete addObject:notif];
}
}];
[self.currentNotifications removeObjectsInArray:toDelete];
- (void)cancelNotificationsWithThreadId:(NSString *)threadId
{
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableArray *toDelete = [NSMutableArray array];
[self.currentNotifications
enumerateObjectsUsingBlock:^(UILocalNotification *notif, NSUInteger idx, BOOL *stop) {
if ([notif.userInfo[Signal_Thread_UserInfo_Key] isEqualToString:threadId]) {
[[UIApplication sharedApplication] cancelLocalNotification:notif];
[toDelete addObject:notif];
}
}];
[self.currentNotifications removeObjectsInArray:toDelete];
});
}
+ (NSString *)tag

Loading…
Cancel
Save