diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index bc8348e08..9be20ed8a 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -523,7 +523,7 @@ static NSString *const kURLHostVerifyPrefix = @"verify"; // Avoid blocking app launch by putting all further possible DB access in async block dispatch_async(dispatch_get_main_queue(), ^{ [TSSocketManager requestSocketOpen]; - [[Environment getCurrent].contactsManager fetchSystemContactsIfAlreadyAuthorized]; + [[Environment getCurrent].contactsManager fetchSystemContactsOnceIfAlreadyAuthorized]; // This will fetch new messages, if we're using domain fronting. [[PushManager sharedManager] applicationDidBecomeActive]; diff --git a/Signal/src/contact/OWSContactsManager.h b/Signal/src/contact/OWSContactsManager.h index 10b965175..c10f8c2de 100644 --- a/Signal/src/contact/OWSContactsManager.h +++ b/Signal/src/contact/OWSContactsManager.h @@ -71,7 +71,9 @@ extern NSString *const OWSContactsManagerSignalAccountsDidChangeNotification; // Ensure's the app has the latest contacts, but won't prompt the user for contact // access if they haven't granted it. -- (void)fetchSystemContactsIfAlreadyAuthorized; +- (void)fetchSystemContactsOnceIfAlreadyAuthorized; +// This variant will fetch system contacts if contact access has already been granted, +// but not prompt for contact access. Also, it will always fire a notification. - (void)fetchSystemContactsIfAlreadyAuthorizedAndAlwaysNotify; #pragma mark - Util diff --git a/Signal/src/contact/OWSContactsManager.m b/Signal/src/contact/OWSContactsManager.m index 230951820..3e4c3604a 100644 --- a/Signal/src/contact/OWSContactsManager.m +++ b/Signal/src/contact/OWSContactsManager.m @@ -93,14 +93,14 @@ NSString *const kTSStorageManager_lastKnownContactRecipientIds = @"lastKnownCont [self.systemContactsFetcher requestOnceWithCompletion:completion]; } -- (void)fetchSystemContactsIfAlreadyAuthorized +- (void)fetchSystemContactsOnceIfAlreadyAuthorized { - [self.systemContactsFetcher fetchIfAlreadyAuthorizedWithAlwaysNotify:NO]; + [self.systemContactsFetcher fetchOnceIfAlreadyAuthorized]; } - (void)fetchSystemContactsIfAlreadyAuthorizedAndAlwaysNotify { - [self.systemContactsFetcher fetchIfAlreadyAuthorizedWithAlwaysNotify:YES]; + [self.systemContactsFetcher fetchIfAlreadyAuthorizedAndAlwaysNotify]; } - (BOOL)isSystemContactsAuthorized diff --git a/Signal/src/contact/SystemContactsFetcher.swift b/Signal/src/contact/SystemContactsFetcher.swift index ba5d0b847..b3d92aa9a 100644 --- a/Signal/src/contact/SystemContactsFetcher.swift +++ b/Signal/src/contact/SystemContactsFetcher.swift @@ -344,7 +344,6 @@ class SystemContactsFetcher: NSObject { private var systemContactsHaveBeenRequestedAtLeastOnce = false private var hasSetupObservation = false - private var isFetchingContacts = false override init() { self.contactStoreAdapter = ContactStoreAdapter() @@ -362,8 +361,7 @@ class SystemContactsFetcher: NSObject { hasSetupObservation = true self.contactStoreAdapter.startObservingChanges { [weak self] in DispatchQueue.main.async { - // If contacts have changed, don't de-bounce. - self?.updateContacts(completion: nil, alwaysNotify: false, shouldDebounce: false) + self?.updateContacts(completion: nil, alwaysNotify: false) } } } @@ -382,7 +380,6 @@ class SystemContactsFetcher: NSObject { completion?(nil) return } - systemContactsHaveBeenRequestedAtLeastOnce = true setupObservationIfNecessary() switch authorizationStatus { @@ -419,33 +416,28 @@ class SystemContactsFetcher: NSObject { } } - public func fetchIfAlreadyAuthorized(alwaysNotify: Bool = false) { + public func fetchOnceIfAlreadyAuthorized() { AssertIsOnMainThread() guard authorizationStatus == .authorized else { return } + guard !systemContactsHaveBeenRequestedAtLeastOnce else { + return + } - updateContacts(completion: nil, alwaysNotify:alwaysNotify) + updateContacts(completion: nil, alwaysNotify:false) } - private func tryToAcquireContactFetchLock() -> Bool { - var didAcquireLock = false - objc_sync_enter(self) - if !self.isFetchingContacts { - self.isFetchingContacts = true - didAcquireLock = true + public func fetchIfAlreadyAuthorizedAndAlwaysNotify() { + AssertIsOnMainThread() + guard authorizationStatus == .authorized else { + return } - objc_sync_exit(self) - return didAcquireLock - } - private func releaseContactFetchLock() { - objc_sync_enter(self) - self.isFetchingContacts = false - objc_sync_exit(self) + updateContacts(completion: nil, alwaysNotify:true) } - private func updateContacts(completion: ((Error?) -> Void)?, alwaysNotify: Bool = false, shouldDebounce: Bool = true) { + private func updateContacts(completion: ((Error?) -> Void)?, alwaysNotify: Bool = false) { AssertIsOnMainThread() systemContactsHaveBeenRequestedAtLeastOnce = true @@ -453,18 +445,6 @@ class SystemContactsFetcher: NSObject { DispatchQueue.global().async { - if shouldDebounce { - guard self.tryToAcquireContactFetchLock() else { - Logger.info("\(self.TAG) ignoring redundant system contacts fetch.") - return - } - } - defer { - if shouldDebounce { - self.releaseContactFetchLock() - } - } - Logger.info("\(self.TAG) fetching contacts") var fetchedContacts: [Contact]?