Respond to CR.

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

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

@ -71,7 +71,9 @@ 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;
// 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; - (void)fetchSystemContactsIfAlreadyAuthorizedAndAlwaysNotify;
#pragma mark - Util #pragma mark - Util

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

@ -344,7 +344,6 @@ class SystemContactsFetcher: NSObject {
private var systemContactsHaveBeenRequestedAtLeastOnce = false private var systemContactsHaveBeenRequestedAtLeastOnce = false
private var hasSetupObservation = false private var hasSetupObservation = false
private var isFetchingContacts = false
override init() { override init() {
self.contactStoreAdapter = ContactStoreAdapter() self.contactStoreAdapter = ContactStoreAdapter()
@ -362,8 +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 {
// If contacts have changed, don't de-bounce. self?.updateContacts(completion: nil, alwaysNotify: false)
self?.updateContacts(completion: nil, alwaysNotify: false, shouldDebounce: false)
} }
} }
} }
@ -382,7 +380,6 @@ class SystemContactsFetcher: NSObject {
completion?(nil) completion?(nil)
return return
} }
systemContactsHaveBeenRequestedAtLeastOnce = true
setupObservationIfNecessary() setupObservationIfNecessary()
switch authorizationStatus { switch authorizationStatus {
@ -419,33 +416,28 @@ class SystemContactsFetcher: NSObject {
} }
} }
public func fetchIfAlreadyAuthorized(alwaysNotify: Bool = false) { public func fetchOnceIfAlreadyAuthorized() {
AssertIsOnMainThread() AssertIsOnMainThread()
guard authorizationStatus == .authorized else { guard authorizationStatus == .authorized else {
return return
} }
guard !systemContactsHaveBeenRequestedAtLeastOnce else {
return
}
updateContacts(completion: nil, alwaysNotify:alwaysNotify) updateContacts(completion: nil, alwaysNotify:false)
} }
private func tryToAcquireContactFetchLock() -> Bool { public func fetchIfAlreadyAuthorizedAndAlwaysNotify() {
var didAcquireLock = false AssertIsOnMainThread()
objc_sync_enter(self) guard authorizationStatus == .authorized else {
if !self.isFetchingContacts { return
self.isFetchingContacts = true
didAcquireLock = true
} }
objc_sync_exit(self)
return didAcquireLock
}
private func releaseContactFetchLock() { updateContacts(completion: nil, alwaysNotify:true)
objc_sync_enter(self)
self.isFetchingContacts = false
objc_sync_exit(self)
} }
private func updateContacts(completion: ((Error?) -> Void)?, alwaysNotify: Bool = false, shouldDebounce: Bool = true) { private func updateContacts(completion: ((Error?) -> Void)?, alwaysNotify: Bool = false) {
AssertIsOnMainThread() AssertIsOnMainThread()
systemContactsHaveBeenRequestedAtLeastOnce = true systemContactsHaveBeenRequestedAtLeastOnce = true
@ -453,18 +445,6 @@ class SystemContactsFetcher: NSObject {
DispatchQueue.global().async { 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") Logger.info("\(self.TAG) fetching contacts")
var fetchedContacts: [Contact]? var fetchedContacts: [Contact]?

Loading…
Cancel
Save