diff --git a/MailCore/API/SyncedAuthenticator.swift b/MailCore/API/SyncedAuthenticator.swift
index 88e7f633f..eaae3c753 100644
--- a/MailCore/API/SyncedAuthenticator.swift
+++ b/MailCore/API/SyncedAuthenticator.swift
@@ -97,28 +97,21 @@ final class SyncedAuthenticator: OAuthAuthenticator {
}
}
- // It is absolutely necessary that the app stays awake while we refresh the token
- BackgroundExecutor.executeWithBackgroundTask { endBackgroundTask in
- self.networkLoginService.refreshToken(token: credential) { token, error in
- // New token has been fetched correctly
- if let token {
- SentrySDK
- .addBreadcrumb(token
- .generateBreadcrumb(level: .info, message: "Refreshing token - Success with remote"))
- self.refreshTokenDelegate?.didUpdateToken(newToken: token, oldToken: credential)
- completion(.success(token))
- } else {
- completion(self.handleFailedRefreshingToken(oldToken: credential, error: error))
- }
- endBackgroundTask()
+ // It is necessary that the app stays awake while we refresh the token
+ let expiringActivity = ExpiringActivity()
+ expiringActivity.start()
+ networkLoginService.refreshToken(token: credential) { token, error in
+ // New token has been fetched correctly
+ if let token {
+ SentrySDK
+ .addBreadcrumb(token
+ .generateBreadcrumb(level: .info, message: "Refreshing token - Success with remote"))
+ self.refreshTokenDelegate?.didUpdateToken(newToken: token, oldToken: credential)
+ completion(.success(token))
+ } else {
+ completion(self.handleFailedRefreshingToken(oldToken: credential, error: error))
}
- } onExpired: {
- SentrySDK
- .addBreadcrumb((credential as ApiToken)
- .generateBreadcrumb(level: .error, message: "Refreshing token failed - Background task expired"))
- // If we didn't fetch the new token in the given time there is not much we can do apart from hoping that it wasn't
- // revoked
- completion(.failure(MailError.noToken))
+ expiringActivity.endAll()
}
}
}
diff --git a/MailCore/Cache/BackgroundExecutor.swift b/MailCore/Cache/BackgroundExecutor.swift
deleted file mode 100644
index 9876660ff..000000000
--- a/MailCore/Cache/BackgroundExecutor.swift
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- Infomaniak Mail - iOS App
- Copyright (C) 2022 Infomaniak Network SA
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program. If not, see .
- */
-
-import CocoaLumberjackSwift
-import Foundation
-
-enum BackgroundExecutor {
- typealias TaskCompletion = () -> Void
- static func executeWithBackgroundTask(_ block: @escaping (@escaping TaskCompletion) -> Void,
- onExpired: @escaping () -> Void) {
- let taskName = "executeWithBackgroundTask \(UUID().uuidString)"
- DDLogDebug("Starting task \(taskName)")
- let processInfos = ProcessInfo()
- let group = TolerantDispatchGroup()
- group.enter()
- processInfos.performExpiringActivity(withReason: taskName) { expired in
- if expired {
- onExpired()
- DDLogDebug("Expired task \(taskName)")
- group.leave()
- } else {
- block {
- DDLogDebug("Ending task \(taskName)")
- group.leave()
- }
- group.wait()
- }
- }
- }
-}
diff --git a/MailCore/Utils/Model/Realm/BackgroundRealm.swift b/MailCore/Utils/Model/Realm/BackgroundRealm.swift
index 4a83061e7..ccafdc5cb 100644
--- a/MailCore/Utils/Model/Realm/BackgroundRealm.swift
+++ b/MailCore/Utils/Model/Realm/BackgroundRealm.swift
@@ -17,6 +17,7 @@
*/
import Foundation
+import InfomaniakCore
import RealmSwift
import Sentry
@@ -38,16 +39,12 @@ public final class BackgroundRealm {
}
public func execute(_ block: @escaping (Realm) -> T, completion: @escaping (T) -> Void) {
- BackgroundExecutor.executeWithBackgroundTask { [weak self] taskCompleted in
- self?.queue.async {
- guard let realm = self?.getRealm() else { return }
- completion(block(realm))
- taskCompleted()
- }
- } onExpired: {
- let expiredBreadcrumb = Breadcrumb(level: .warning, category: "BackgroundRealm")
- expiredBreadcrumb.message = "Task expired before completing"
- SentrySDK.addBreadcrumb(expiredBreadcrumb)
+ let expiringActivity = ExpiringActivity()
+ expiringActivity.start()
+ queue.async {
+ let realm = self.getRealm()
+ completion(block(realm))
+ expiringActivity.endAll()
}
}