From 36913b425cfbfe341687ac586324437c44274356 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Thu, 2 Mar 2023 15:23:23 -0600 Subject: [PATCH] =?UTF-8?q?Fixes=20=E2=80=9Cvariable=20mutated=20after=20c?= =?UTF-8?q?apture=20by=20sendable=20closure=E2=80=9D=20warning?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AltStore/Operations/BackupAppOperation.swift | 21 ++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/AltStore/Operations/BackupAppOperation.swift b/AltStore/Operations/BackupAppOperation.swift index 2cf17266..27fbb793 100644 --- a/AltStore/Operations/BackupAppOperation.swift +++ b/AltStore/Operations/BackupAppOperation.swift @@ -29,6 +29,9 @@ class BackupAppOperation: ResultOperation private var appName: String? private var timeoutTimer: Timer? + private weak var applicationWillReturnObserver: NSObjectProtocol? + private weak var backupResponseObserver: NSObjectProtocol? + init(action: Action, context: InstallAppOperationContext) { self.action = action @@ -153,8 +156,11 @@ private extension BackupAppOperation { func registerObservers() { - var applicationWillReturnObserver: NSObjectProtocol! - applicationWillReturnObserver = NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: .main) { [weak self] (notification) in + self.applicationWillReturnObserver = NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: .main) { [weak self] (notification) in + defer { + self?.applicationWillReturnObserver.map { NotificationCenter.default.removeObserver($0) } + } + guard let self = self, !self.isFinished else { return } self.timeoutTimer = Timer.scheduledTimer(withTimeInterval: 5, repeats: false) { [weak self] (timer) in @@ -166,18 +172,17 @@ private extension BackupAppOperation self.finish(.failure(OperationError.timedOut)) } } - - NotificationCenter.default.removeObserver(applicationWillReturnObserver!) } - var backupResponseObserver: NSObjectProtocol! - backupResponseObserver = NotificationCenter.default.addObserver(forName: AppDelegate.appBackupDidFinish, object: nil, queue: nil) { [weak self] (notification) in + self.backupResponseObserver = NotificationCenter.default.addObserver(forName: AppDelegate.appBackupDidFinish, object: nil, queue: nil) { [weak self] (notification) in + defer { + self?.backupResponseObserver.map { NotificationCenter.default.removeObserver($0) } + } + self?.timeoutTimer?.invalidate() let result = notification.userInfo?[AppDelegate.appBackupResultKey] as? Result ?? .failure(OperationError.unknownResult) self?.finish(result) - - NotificationCenter.default.removeObserver(backupResponseObserver!) } } }