Fix race condition

pull/233/head
nielsandriesse 5 years ago
parent f1581b4edf
commit a16f30dc39

@ -16,29 +16,31 @@ final class NotificationServiceExtension : UNNotificationServiceExtension {
self.contentHandler = contentHandler self.contentHandler = contentHandler
notificationContent = (request.content.mutableCopy() as? UNMutableNotificationContent) notificationContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
DispatchQueue.main.sync { self.setUpIfNecessary() } DispatchQueue.main.async {
self.setUpIfNecessary() {
if let notificationContent = notificationContent { if let notificationContent = self.notificationContent {
// Modify the notification content here... // Modify the notification content here...
let base64EncodedData = notificationContent.userInfo["ENCRYPTED_DATA"] as! String let base64EncodedData = notificationContent.userInfo["ENCRYPTED_DATA"] as! String
let data = Data(base64Encoded: base64EncodedData)! let data = Data(base64Encoded: base64EncodedData)!
let decrypter = SSKEnvironment.shared.messageDecrypter let decrypter = SSKEnvironment.shared.messageDecrypter
if let envelope = try? MessageWrapper.unwrap(data: data), let data = try? envelope.serializedData() { if let envelope = try? MessageWrapper.unwrap(data: data), let data = try? envelope.serializedData() {
decrypter.decryptEnvelope(envelope, decrypter.decryptEnvelope(envelope,
envelopeData: data, envelopeData: data,
successBlock: { result, transaction in successBlock: { result, transaction in
if (try? SSKProtoEnvelope.parseData(result.envelopeData)) != nil { if (try? SSKProtoEnvelope.parseData(result.envelopeData)) != nil {
self.handleDecryptionResult(result: result, notificationContent: notificationContent, transaction: transaction) self.handleDecryptionResult(result: result, notificationContent: notificationContent, transaction: transaction)
} else { } else {
self.completeWithFailure(content: notificationContent) self.completeWithFailure(content: notificationContent)
} }
}, },
failureBlock: { failureBlock: {
self.completeWithFailure(content: notificationContent) self.completeWithFailure(content: notificationContent)
} }
) )
} else { } else {
self.completeWithFailure(content: notificationContent) self.completeWithFailure(content: notificationContent)
}
}
} }
} }
} }
@ -96,7 +98,7 @@ final class NotificationServiceExtension : UNNotificationServiceExtension {
} }
} }
func setUpIfNecessary() { func setUpIfNecessary(completion: @escaping () -> Void) {
AssertIsOnMainThread() AssertIsOnMainThread()
// The NSE will often re-use the same process, so if we're // The NSE will often re-use the same process, so if we're
@ -132,6 +134,7 @@ final class NotificationServiceExtension : UNNotificationServiceExtension {
}, },
migrationCompletion: { [weak self] in migrationCompletion: { [weak self] in
self?.versionMigrationsDidComplete() self?.versionMigrationsDidComplete()
completion()
} }
) )

Loading…
Cancel
Save