Use long-lived operations for CK backup.

This commit is contained in:
Matthew Chen 2018-11-21 16:25:52 -08:00
parent 018fe6cb42
commit ba3a1863da
2 changed files with 42 additions and 0 deletions

View File

@ -109,6 +109,8 @@ NSString *NSStringForBackupImportState(OWSBackupState state)
- (void)setup
{
[OWSBackupAPI setup];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidBecomeActive:)
name:OWSApplicationDidBecomeActiveNotification

View File

@ -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()
})
}
}
}
}