From ba3a1863dac51e9904bbda230bc839a8b1f4a389 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 21 Nov 2018 16:25:52 -0800 Subject: [PATCH] Use long-lived operations for CK backup. --- Signal/src/util/Backup/OWSBackup.m | 2 ++ Signal/src/util/Backup/OWSBackupAPI.swift | 40 +++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/Signal/src/util/Backup/OWSBackup.m b/Signal/src/util/Backup/OWSBackup.m index 9d43e6156..550ffead3 100644 --- a/Signal/src/util/Backup/OWSBackup.m +++ b/Signal/src/util/Backup/OWSBackup.m @@ -109,6 +109,8 @@ NSString *NSStringForBackupImportState(OWSBackupState state) - (void)setup { + [OWSBackupAPI setup]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:OWSApplicationDidBecomeActiveNotification diff --git a/Signal/src/util/Backup/OWSBackupAPI.swift b/Signal/src/util/Backup/OWSBackupAPI.swift index 6320d482c..e7a7db1a8 100644 --- a/Signal/src/util/Backup/OWSBackupAPI.swift +++ b/Signal/src/util/Backup/OWSBackupAPI.swift @@ -626,4 +626,44 @@ import CloudKit return .success } } + + // MARK: - + + @objc + public class func setup() { + cancelAllLongLivedOperations() + } + + private class func cancelAllLongLivedOperations() { + // These APIs are only available in iOS 9.3 and later. + guard #available(iOS 9.3, *) else { + return + } + + let container = CKContainer.default() + container.fetchAllLongLivedOperationIDs { (operationIds, error) in + if let error = error { + Logger.error("Could not get all long lived operations: \(error)") + return + } + guard let operationIds = operationIds else { + Logger.error("No operation ids.") + return + } + + for operationId in operationIds { + container.fetchLongLivedOperation(withID: operationId, completionHandler: { (operation, error) in + if let error = error { + Logger.error("Could not get long lived operation [\(operationId)]: \(error)") + return + } + guard let operation = operation else { + Logger.error("No operation.") + return + } + operation.cancel() + }) + } + } + } }