Merge branch 'charlesmchen/oneSystemContactsFetchAtATime'

pull/1/head
Matthew Chen 8 years ago
commit 97bab48a93

@ -523,7 +523,7 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
// Avoid blocking app launch by putting all further possible DB access in async block // Avoid blocking app launch by putting all further possible DB access in async block
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[TSSocketManager requestSocketOpen]; [TSSocketManager requestSocketOpen];
[[Environment getCurrent].contactsManager fetchSystemContactsIfAlreadyAuthorized]; [[Environment getCurrent].contactsManager fetchSystemContactsOnceIfAlreadyAuthorized];
// This will fetch new messages, if we're using domain fronting. // This will fetch new messages, if we're using domain fronting.
[[PushManager sharedManager] applicationDidBecomeActive]; [[PushManager sharedManager] applicationDidBecomeActive];

@ -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];
} }

@ -71,8 +71,10 @@ 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)fetchSystemContactsOnceIfAlreadyAuthorized;
- (void)fetchSystemContactsIfAlreadyAuthorizedAndIgnoreDebounce; // 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 #pragma mark - Util

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

@ -361,7 +361,7 @@ 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) self?.updateContacts(completion: nil, alwaysNotify: false)
} }
} }
} }
@ -380,7 +380,6 @@ class SystemContactsFetcher: NSObject {
completion?(nil) completion?(nil)
return return
} }
systemContactsHaveBeenRequestedAtLeastOnce = true
setupObservationIfNecessary() setupObservationIfNecessary()
switch authorizationStatus { switch authorizationStatus {
@ -417,16 +416,28 @@ class SystemContactsFetcher: NSObject {
} }
} }
public func fetchIfAlreadyAuthorized(ignoreDebounce: Bool = false) { public func fetchOnceIfAlreadyAuthorized() {
AssertIsOnMainThread() AssertIsOnMainThread()
guard authorizationStatus == .authorized else { guard authorizationStatus == .authorized else {
return return
} }
guard !systemContactsHaveBeenRequestedAtLeastOnce else {
return
}
updateContacts(completion: nil, ignoreDebounce:ignoreDebounce) updateContacts(completion: nil, alwaysNotify:false)
} }
private func updateContacts(completion: ((Error?) -> Void)?, ignoreDebounce: Bool = false) { public func fetchIfAlreadyAuthorizedAndAlwaysNotify() {
AssertIsOnMainThread()
guard authorizationStatus == .authorized else {
return
}
updateContacts(completion: nil, alwaysNotify:true)
}
private func updateContacts(completion: ((Error?) -> Void)?, alwaysNotify: Bool = false) {
AssertIsOnMainThread() AssertIsOnMainThread()
systemContactsHaveBeenRequestedAtLeastOnce = true systemContactsHaveBeenRequestedAtLeastOnce = true
@ -434,8 +445,9 @@ class SystemContactsFetcher: NSObject {
DispatchQueue.global().async { DispatchQueue.global().async {
var fetchedContacts: [Contact]? Logger.info("\(self.TAG) fetching contacts")
var fetchedContacts: [Contact]?
switch self.contactStoreAdapter.fetchContacts() { switch self.contactStoreAdapter.fetchContacts() {
case .success(let result): case .success(let result):
fetchedContacts = result fetchedContacts = result
@ -457,7 +469,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