oOnly one system contacts fetch at a time.

pull/1/head
Matthew Chen 8 years ago
parent e1a078900f
commit b767996dba

@ -146,6 +146,9 @@ public class SystemContactsFetcher: NSObject {
public private(set) var systemContactsHaveBeenRequestedAtLeastOnce = false public private(set) var systemContactsHaveBeenRequestedAtLeastOnce = false
private var hasSetupObservation = false private var hasSetupObservation = false
private var isFetching = false
private var hasQueuedFetch = false
override init() { override init() {
self.contactStoreAdapter = ContactsFrameworkContactStoreAdaptee() self.contactStoreAdapter = ContactsFrameworkContactStoreAdaptee()
@ -232,6 +235,7 @@ public class SystemContactsFetcher: NSObject {
@objc @objc
public func fetchOnceIfAlreadyAuthorized() { public func fetchOnceIfAlreadyAuthorized() {
SwiftAssertIsOnMainThread(#function) SwiftAssertIsOnMainThread(#function)
guard authorizationStatus == .authorized else { guard authorizationStatus == .authorized else {
return return
} }
@ -269,11 +273,25 @@ public class SystemContactsFetcher: NSObject {
Logger.error("background task time ran out contacts fetch completed.") Logger.error("background task time ran out contacts fetch completed.")
}) })
guard !isFetching else {
Logger.info("\(self.TAG) queuing contact fetch; contact fetch already in flight.")
hasQueuedFetch = true
return
}
isFetching = true
// Ensure completion is invoked on main thread. // Ensure completion is invoked on main thread.
let completion: (Error?) -> Void = { error in let completion: (Error?) -> Void = { error in
DispatchMainThreadSafe({ DispatchMainThreadSafe({
self.isFetching = false
completionParam?(error) completionParam?(error)
backgroundTask = nil backgroundTask = nil
if self.hasQueuedFetch {
self.hasQueuedFetch = false
self.fetchOnceIfAlreadyAuthorized()
}
}) })
} }

Loading…
Cancel
Save