Respond to CR.

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

@ -146,7 +146,7 @@ NS_ASSUME_NONNULL_BEGIN
{ {
OWSAssert([NSThread isMainThread]); OWSAssert([NSThread isMainThread]);
[self.contactsViewHelper.contactsManager fetchSystemContactsIfAlreadyAuthorizedAndIgnoreDebounce]; [self.contactsViewHelper.contactsManager fetchSystemContactsIfAlreadyAuthorizedAndAlwaysNotify];
[refreshControl endRefreshing]; [refreshControl endRefreshing];
} }

@ -72,7 +72,7 @@ extern NSString *const OWSContactsManagerSignalAccountsDidChangeNotification;
// Ensure's the app has the latest contacts, but won't prompt the user for contact // Ensure's the app has the latest contacts, but won't prompt the user for contact
// access if they haven't granted it. // access if they haven't granted it.
- (void)fetchSystemContactsIfAlreadyAuthorized; - (void)fetchSystemContactsIfAlreadyAuthorized;
- (void)fetchSystemContactsIfAlreadyAuthorizedAndIgnoreDebounce; - (void)fetchSystemContactsIfAlreadyAuthorizedAndAlwaysNotify;
#pragma mark - Util #pragma mark - Util

@ -93,15 +93,14 @@ NSString *const kTSStorageManager_lastKnownContactRecipientIds = @"lastKnownCont
[self.systemContactsFetcher requestOnceWithCompletion:completion]; [self.systemContactsFetcher requestOnceWithCompletion:completion];
} }
- (void)fetchSystemContactsIfAlreadyAuthorized - (void)fetchSystemContactsIfAlreadyAuthorized
{ {
[self.systemContactsFetcher fetchIfAlreadyAuthorizedWithIgnoreDebounce:NO]; [self.systemContactsFetcher fetchIfAlreadyAuthorizedWithAlwaysNotify:NO];
} }
- (void)fetchSystemContactsIfAlreadyAuthorizedAndIgnoreDebounce - (void)fetchSystemContactsIfAlreadyAuthorizedAndAlwaysNotify
{ {
[self.systemContactsFetcher fetchIfAlreadyAuthorizedWithIgnoreDebounce:YES]; [self.systemContactsFetcher fetchIfAlreadyAuthorizedWithAlwaysNotify:YES];
} }
- (BOOL)isSystemContactsAuthorized - (BOOL)isSystemContactsAuthorized

@ -362,7 +362,8 @@ class SystemContactsFetcher: NSObject {
hasSetupObservation = true hasSetupObservation = true
self.contactStoreAdapter.startObservingChanges { [weak self] in self.contactStoreAdapter.startObservingChanges { [weak self] in
DispatchQueue.main.async { DispatchQueue.main.async {
self?.updateContacts(completion: nil) // If contacts have changed, don't de-bounce.
self?.updateContacts(completion: nil, alwaysNotify: false, shouldDebounce: false)
} }
} }
} }
@ -418,13 +419,13 @@ class SystemContactsFetcher: NSObject {
} }
} }
public func fetchIfAlreadyAuthorized(ignoreDebounce: Bool = false) { public func fetchIfAlreadyAuthorized(alwaysNotify: Bool = false) {
AssertIsOnMainThread() AssertIsOnMainThread()
guard authorizationStatus == .authorized else { guard authorizationStatus == .authorized else {
return return
} }
updateContacts(completion: nil, ignoreDebounce:ignoreDebounce) updateContacts(completion: nil, alwaysNotify:alwaysNotify)
} }
private func tryToAcquireContactFetchLock() -> Bool { private func tryToAcquireContactFetchLock() -> Bool {
@ -444,7 +445,7 @@ class SystemContactsFetcher: NSObject {
objc_sync_exit(self) objc_sync_exit(self)
} }
private func updateContacts(completion: ((Error?) -> Void)?, ignoreDebounce: Bool = false) { private func updateContacts(completion: ((Error?) -> Void)?, alwaysNotify: Bool = false, shouldDebounce: Bool = true) {
AssertIsOnMainThread() AssertIsOnMainThread()
systemContactsHaveBeenRequestedAtLeastOnce = true systemContactsHaveBeenRequestedAtLeastOnce = true
@ -452,10 +453,19 @@ class SystemContactsFetcher: NSObject {
DispatchQueue.global().async { DispatchQueue.global().async {
if shouldDebounce {
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
} }
}
defer {
if shouldDebounce {
self.releaseContactFetchLock()
}
}
Logger.info("\(self.TAG) fetching contacts")
var fetchedContacts: [Contact]? var fetchedContacts: [Contact]?
switch self.contactStoreAdapter.fetchContacts() { switch self.contactStoreAdapter.fetchContacts() {
@ -463,10 +473,8 @@ class SystemContactsFetcher: NSObject {
fetchedContacts = result fetchedContacts = result
case .error(let error): case .error(let error):
completion?(error) completion?(error)
self.releaseContactFetchLock()
return return
} }
self.releaseContactFetchLock()
guard let contacts = fetchedContacts else { guard let contacts = fetchedContacts else {
owsFail("\(self.TAG) contacts was unexpectedly not set.") owsFail("\(self.TAG) contacts was unexpectedly not set.")
@ -481,7 +489,7 @@ class SystemContactsFetcher: NSObject {
if self.lastContactUpdateHash != contactsHash { if self.lastContactUpdateHash != contactsHash {
Logger.info("\(self.TAG) contact hash changed. new contactsHash: \(contactsHash)") Logger.info("\(self.TAG) contact hash changed. new contactsHash: \(contactsHash)")
shouldNotifyDelegate = true shouldNotifyDelegate = true
} else if ignoreDebounce { } else if alwaysNotify {
Logger.info("\(self.TAG) ignoring debounce.") Logger.info("\(self.TAG) ignoring debounce.")
shouldNotifyDelegate = true shouldNotifyDelegate = true
} else { } else {

Loading…
Cancel
Save