From d1141581de9455ffd1e1d002782c81d7798ff905 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 23 Oct 2017 15:56:06 -0400 Subject: [PATCH] Only one system contacts fetch at a time. // FREEBIE --- Signal/src/contact/SystemContactsFetcher.swift | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Signal/src/contact/SystemContactsFetcher.swift b/Signal/src/contact/SystemContactsFetcher.swift index 9b80df975..00664d475 100644 --- a/Signal/src/contact/SystemContactsFetcher.swift +++ b/Signal/src/contact/SystemContactsFetcher.swift @@ -345,7 +345,6 @@ class SystemContactsFetcher: NSObject { private var systemContactsHaveBeenRequestedAtLeastOnce = false private var hasSetupObservation = false private var isFetchingContacts = false - private static let serialQueue = DispatchQueue(label: "org.whispersystems.system.contacts") override init() { self.contactStoreAdapter = ContactStoreAdapter() @@ -430,20 +429,19 @@ class SystemContactsFetcher: NSObject { private func tryToAcquireContactFetchLock() -> Bool { var didAcquireLock = false - SystemContactsFetcher.serialQueue.sync { - guard !self.isFetchingContacts else { - return - } + objc_sync_enter(self) + if !self.isFetchingContacts { self.isFetchingContacts = true didAcquireLock = true } + objc_sync_exit(self) return didAcquireLock } private func releaseContactFetchLock() { - SystemContactsFetcher.serialQueue.sync { - self.isFetchingContacts = false - } + objc_sync_enter(self) + self.isFetchingContacts = false + objc_sync_exit(self) } private func updateContacts(completion: ((Error?) -> Void)?, ignoreDebounce: Bool = false) { @@ -454,13 +452,12 @@ class SystemContactsFetcher: NSObject { DispatchQueue.global().async { - var fetchedContacts: [Contact]? - guard self.tryToAcquireContactFetchLock() else { Logger.info("\(self.TAG) ignoring redundant system contacts fetch.") return } + var fetchedContacts: [Contact]? switch self.contactStoreAdapter.fetchContacts() { case .success(let result): fetchedContacts = result