I want to add ABRecordRef to existing Group. Some how its not working. Here is my code:
-(void)addUsers:(NSMutableArray*)users toGroupID:(ABRecordID)groupID {
CFErrorRef error = NULL;
ABAddressBookRef addressBookRef = ABAddressBookCreate();
// Get Group
ABRecordRef group = ABAddressBookGetGroupWithRecordID(addressBookRef,groupID);
for (User *user in users) {
int recordId = user.uniqID;
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBookRef,(ABRecordID)recordId);
//add the new person to the record
ABAddressBookAddRecord(addressBookRef, person, nil);
ABAddressBookSave(addressBookRef, &error);
// add the group
ABAddressBookAddRecord(addressBookRef, group, &error);
ABAddressBookSave(addressBookRef, &error);
// add the person to the group
ABGroupAddMember(group, person, &error);
ABAddressBookSave(addressBookRef, &error);
//save the record
ABAddressBookSave(addressBookRef, nil);
if (!isMemberAdded) {
[self showAlertWithTitle:@"Alert" andMessage:@"Some Error Occured While Adding User to Existing Group"];
}
}
BOOL isMembersAddedToGroup = ABAddressBookSave(addressBookRef, nil);
if (isMembersAddedToGroup) {
[self showAlertWithTitle:@"Alert" andMessage:@"Selected Members Added to Existing Group Successfully"];
}
else{
[self showAlertWithTitle:@"Alert" andMessage:@"Some Error Occured While Adding User to Existing Group"];
}
CFRelease(addressBookRef);
}
Whenever I use above code & try to save it in existing group I get following message in console
CPSqliteStatementPerform: constraint failed for INSERT INTO ABGroupMembers (group_id, member_type, member_id) VALUES (?, ?, ?);
CPSqliteStatementReset: columns group_id, member_type, member_id are not unique for INSERT INTO ABGroupMembers (group_id, member_type, member_id) VALUES (?, ?, ?);
I am not getting what I am missing in above code. Any knid of help is appreciated. Thanks
If we see the message:
Also in documentation :
(So, internally, this error is caused due to incorrect sqlite query.)
It indicates that there is some issue with
ABGroupAddMemberorABAddressBookAddRecord(addressBookRef, group, &error);call.The problem can be: We have group and person references already with us when we call
ABAddressBookGetGroupWithRecordIDandABAddressBookGetGroupWithRecordIDfunctions.Now, when
ABAddressBookAddRecordis executed for both(personandgroup) it is adding them toaddressbookbut not updating their UniqueIds(id for person and group ref.) so at the time ofABGroupAddMemberfunction call it haspersonandgroupwith same ids which are added inABGroupMemberstable.Please, try to print
recordIdforpersonandgroupafter you add them to address book(and before you add them to group). ifrecordsIdsare not updated then that is the cause, and you will need to fetchrecordfor newrecordId. Also check whether the newpersonandgroupare being added to addresbook or not.*Currently, I do not mac access to verify my suggestion/answer.