Only one system contacts fetch at a time.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 878fd3d842
commit d1141581de

@ -345,7 +345,6 @@ class SystemContactsFetcher: NSObject {
private var systemContactsHaveBeenRequestedAtLeastOnce = false private var systemContactsHaveBeenRequestedAtLeastOnce = false
private var hasSetupObservation = false private var hasSetupObservation = false
private var isFetchingContacts = false private var isFetchingContacts = false
private static let serialQueue = DispatchQueue(label: "org.whispersystems.system.contacts")
override init() { override init() {
self.contactStoreAdapter = ContactStoreAdapter() self.contactStoreAdapter = ContactStoreAdapter()
@ -430,20 +429,19 @@ class SystemContactsFetcher: NSObject {
private func tryToAcquireContactFetchLock() -> Bool { private func tryToAcquireContactFetchLock() -> Bool {
var didAcquireLock = false var didAcquireLock = false
SystemContactsFetcher.serialQueue.sync { objc_sync_enter(self)
guard !self.isFetchingContacts else { if !self.isFetchingContacts {
return
}
self.isFetchingContacts = true self.isFetchingContacts = true
didAcquireLock = true didAcquireLock = true
} }
objc_sync_exit(self)
return didAcquireLock return didAcquireLock
} }
private func releaseContactFetchLock() { private func releaseContactFetchLock() {
SystemContactsFetcher.serialQueue.sync { objc_sync_enter(self)
self.isFetchingContacts = false self.isFetchingContacts = false
} objc_sync_exit(self)
} }
private func updateContacts(completion: ((Error?) -> Void)?, ignoreDebounce: Bool = false) { private func updateContacts(completion: ((Error?) -> Void)?, ignoreDebounce: Bool = false) {
@ -454,13 +452,12 @@ class SystemContactsFetcher: NSObject {
DispatchQueue.global().async { DispatchQueue.global().async {
var fetchedContacts: [Contact]?
guard self.tryToAcquireContactFetchLock() else { guard self.tryToAcquireContactFetchLock() else {
Logger.info("\(self.TAG) ignoring redundant system contacts fetch.") Logger.info("\(self.TAG) ignoring redundant system contacts fetch.")
return return
} }
var fetchedContacts: [Contact]?
switch self.contactStoreAdapter.fetchContacts() { switch self.contactStoreAdapter.fetchContacts() {
case .success(let result): case .success(let result):
fetchedContacts = result fetchedContacts = result

Loading…
Cancel
Save