0

Anyone know why a change in the contact store triggers more than one notification to a notification center observer?

I did a test project to see how many times CNContactStoreDidChange notification occurs when I make a simple change to contact store.

Here is my relevant code:

NotificationCenter.default.addObserver(self, selector: #selector(self.storeChanged(_:)), name: NSNotification.Name.CNContactStoreDidChange, object: nil)
@objc func storeChanged(_ notification: Notification) {
    
    print("#", #function)
    
}
let newMutableGroup = CNMutableGroup()

newMutableGroup.name = ...

let saveRequest = CNSaveRequest()

saveRequest.add(newMutableGroup, toContainerWithIdentifier: contactStore.defaultContainerIdentifier())

do {
    try contactStore.execute(saveRequest)
} catch {
    print("error:\n\t\(error.localizedDescription)")
}

Debug window output:

# storeChanged(_:)
# storeChanged(_:)
# storeChanged(_:)
# storeChanged(_:)
daniel
  • 1,446
  • 3
  • 29
  • 65

0 Answers0