Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions App/Services/Contacts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ final class ContactsService: Service {

static let shared = ContactsService()

private func runContactStore<T>(_ operation: @escaping () throws -> T) async throws -> T {
try await Task(priority: .utility) {
try operation()
}.value
}

var isActivated: Bool {
get async {
let status = CNContactStore.authorizationStatus(for: .contacts)
Expand Down Expand Up @@ -138,7 +144,9 @@ final class ContactsService: Service {
openWorldHint: false
)
) { _ in
let contact = try self.contactStore.unifiedMeContactWithKeys(toFetch: contactKeys)
let contact = try await self.runContactStore {
try self.contactStore.unifiedMeContactWithKeys(toFetch: contactKeys)
}
return Person(contact)
}

Expand Down Expand Up @@ -206,10 +214,12 @@ final class ContactsService: Service {
? predicates[0]
: NSCompoundPredicate(andPredicateWithSubpredicates: predicates)

let contacts = try self.contactStore.unifiedContacts(
matching: finalPredicate,
keysToFetch: contactKeys
)
let contacts = try await self.runContactStore {
try self.contactStore.unifiedContacts(
matching: finalPredicate,
keysToFetch: contactKeys
)
}

return contacts.compactMap { Person($0) }
}
Expand Down Expand Up @@ -247,7 +257,9 @@ final class ContactsService: Service {
// Fetch the mutable copy of the contact
let predicate = CNContact.predicateForContacts(withIdentifiers: [identifier])
let contact =
try self.contactStore.unifiedContacts(matching: predicate, keysToFetch: contactKeys)
try await self.runContactStore {
try self.contactStore.unifiedContacts(matching: predicate, keysToFetch: contactKeys)
}
.first?
.mutableCopy() as? CNMutableContact

Expand All @@ -270,7 +282,9 @@ final class ContactsService: Service {
saveRequest.update(updatedContact)

// Save the changes
try self.contactStore.execute(saveRequest)
try await self.runContactStore {
try self.contactStore.execute(saveRequest)
}

return Person(updatedContact)
}
Expand Down Expand Up @@ -307,7 +321,9 @@ final class ContactsService: Service {
saveRequest.add(newContact, toContainerWithIdentifier: nil)

// Execute the save request
try self.contactStore.execute(saveRequest)
try await self.runContactStore {
try self.contactStore.execute(saveRequest)
}

return Person(newContact)
}
Expand Down