Skip to content

Commit

Permalink
fix: Limit fetching contacts to every 60 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Feb 27, 2024
1 parent 9a6951a commit 066e2ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Mail/UserAccountScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ struct UserAccountScene: Scene {
guard CNContactStore.authorizationStatus(for: .contacts) != .notDetermined else {
return
}
try await accountManager.currentContactManager?.refreshContactsAndAddressBooks()
try await accountManager.currentContactManager?.refreshContactsAndAddressBooksIfNeeded()
} catch {
DDLogError("Error while updating user account: \(error)")
}
Expand Down
14 changes: 14 additions & 0 deletions MailCore/Cache/ContactManager/ContactManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import SwiftRegex
public typealias ContactManageable = ContactFetchable & ContactManagerCoreable & RealmAccessible

public protocol ContactManagerCoreable {
func refreshContactsAndAddressBooksIfNeeded() async throws
/// Entry point to refresh all contacts in base
func refreshContactsAndAddressBooks() async throws

Expand Down Expand Up @@ -90,6 +91,19 @@ public final class ContactManager: ObservableObject, ContactManageable {

let localContactsHelper = LocalContactsHelper()
var currentMergeRequest: Task<Void, Never>?
var lastRefreshDate: Date?

public func refreshContactsAndAddressBooksIfNeeded() async throws {
let refreshIntervalSeconds = 60.0
if let lastRefreshDate,
lastRefreshDate.addingTimeInterval(refreshIntervalSeconds) > Date() {
DDLogInfo("Skip updating contacts, we updated less than \(Int(refreshIntervalSeconds)) seconds ago")
return
}

try await refreshContactsAndAddressBooks()
lastRefreshDate = Date()
}

public func refreshContactsAndAddressBooks() async throws {
// We do not run an update of contacts in extension mode as we are too resource constrained
Expand Down

0 comments on commit 066e2ba

Please sign in to comment.