Respond to CR.

pull/1/head
Matthew Chen 6 years ago
parent cb349ad0fe
commit 52f52a94a2

@ -51,9 +51,7 @@ public class BackupLazyRestore: NSObject {
// MARK: - // MARK: -
private var backgroundQueue = { private let backgroundQueue = DispatchQueue.global(qos: .background)
DispatchQueue.global(qos: .background)
}()
@objc @objc
public func runIfNecessary() { public func runIfNecessary() {
@ -100,13 +98,12 @@ public class BackupLazyRestore: NSObject {
private func tryToRestoreNextAttachment(attachmentIds: [String], errorCount: UInt, backupIO: OWSBackupIO) { private func tryToRestoreNextAttachment(attachmentIds: [String], errorCount: UInt, backupIO: OWSBackupIO) {
var attachmentIdsCopy = attachmentIds var attachmentIdsCopy = attachmentIds
guard let attachmentId = attachmentIdsCopy.last else { guard let attachmentId = attachmentIdsCopy.popLast() else {
// This job is done. // This job is done.
Logger.verbose("job is done.") Logger.verbose("job is done.")
complete(errorCount: errorCount) complete(errorCount: errorCount)
return return
} }
attachmentIdsCopy.removeLast()
guard let attachmentPointer = TSAttachment.fetch(uniqueId: attachmentId) as? TSAttachmentPointer else { guard let attachmentPointer = TSAttachment.fetch(uniqueId: attachmentId) as? TSAttachmentPointer else {
Logger.warn("could not load attachment.") Logger.warn("could not load attachment.")
// Not necessarily an error. // Not necessarily an error.
@ -117,21 +114,17 @@ public class BackupLazyRestore: NSObject {
} }
backup.lazyRestoreAttachment(attachmentPointer, backup.lazyRestoreAttachment(attachmentPointer,
backupIO: backupIO) backupIO: backupIO)
.done { _ in .done(on: self.backgroundQueue) { _ in
Logger.info("Restored attachment.") Logger.info("Restored attachment.")
self.backgroundQueue.async { // Continue trying to restore the other attachments.
// Continue trying to restore the other attachments. self.tryToRestoreNextAttachment(attachmentIds: attachmentIdsCopy, errorCount: errorCount, backupIO: backupIO)
self.tryToRestoreNextAttachment(attachmentIds: attachmentIdsCopy, errorCount: errorCount, backupIO: backupIO) }.catch(on: self.backgroundQueue) { (error) in
}
}.catch { (error) in
Logger.error("Could not restore attachment: \(error)") Logger.error("Could not restore attachment: \(error)")
self.backgroundQueue.async { // Continue trying to restore the other attachments.
// Continue trying to restore the other attachments. self.tryToRestoreNextAttachment(attachmentIds: attachmentIdsCopy, errorCount: errorCount + 1, backupIO: backupIO)
self.tryToRestoreNextAttachment(attachmentIds: attachmentIdsCopy, errorCount: errorCount + 1, backupIO: backupIO) }.retainUntilComplete()
}
}
} }
private func complete(errorCount: UInt) { private func complete(errorCount: UInt) {

Loading…
Cancel
Save