问题
In Swift, when you update "Contact" by passing CNSaveRequest
to CNContactStore
, the update immediately changes Contacts in your native Contacts app.
However, when you add or delete "Group" (CNGroup
) by passing CNSaveRequest
to CNContactStore
, the addition or deletion doesn't immediately change Groups in your native Contacts app.
In case of addition or deletion of "Group", you have to terminate and restart your app or native Contacts app.
Are there any ways to immediately change Groups in native Contacts app?
For example, in case of Code A, you can immediately see the change in native Contacts app. In case of Code B, you cannot immediately see the change, you have to restart either your app or native Contacts app.
I don't use ABAddressbook
, so naturally we are talking about iOS9.
[Code A]
let req = CNSaveRequest()
req.updateContact(fooContact)
do {
try contactStore.executeSaveRequest(req)
} catch {
print("\(error)")
}
[Code B]
let req = CNSaveRequest()
req.addGroup(mutableGroup)
do {
try contactStore.executeSaveRequest(req)
} catch {
print("\(error)")
}
回答1:
Sorry, but I resolved this problem by myself...
I added the code that refreshes the dataSource
of UITableView
that shows CNGroup
array. For example,...
var groups: [CNGroup] = [] // dataSource of myTableView
......
// Custome function to be called when you want to refresh myTableView
function refreshMyTableView() {
do {
// I forget adding the line below to refresh tableview datasource
try self.groups = contactStore.groupsMatchingPredicate(nil)
myTableView.reloadData()
} catch {
}
}
来源:https://stackoverflow.com/questions/33514970/cnsaverequest-addgroup-and-deletegroup-dont-immediately-change-groups-in-contac