Skip to content

Commit

Permalink
refactor: Remove last uses of BackgroundExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-coye committed May 3, 2024
1 parent ca67d83 commit f416971
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 76 deletions.
35 changes: 14 additions & 21 deletions MailCore/API/SyncedAuthenticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
45 changes: 0 additions & 45 deletions MailCore/Cache/BackgroundExecutor.swift

This file was deleted.

17 changes: 7 additions & 10 deletions MailCore/Utils/Model/Realm/BackgroundRealm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import Foundation
import InfomaniakCore
import RealmSwift
import Sentry

Expand All @@ -38,16 +39,12 @@ public final class BackgroundRealm {
}

public func execute<T>(_ 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()
}
}

Expand Down

0 comments on commit f416971

Please sign in to comment.