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
ABGroupAddMember
orABAddressBookAddRecord(addressBookRef, group, &error);
call.The problem can be: We have group and person references already with us when we call
ABAddressBookGetGroupWithRecordID
andABAddressBookGetGroupWithRecordID
functions.Now, when
ABAddressBookAddRecord
is executed for both(person
andgroup
) it is adding them toaddressbook
but not updating their UniqueIds(id for person and group ref.
) so at the time ofABGroupAddMember
function call it hasperson
andgroup
with same ids which are added inABGroupMembers
table.Please, try to print
recordId
forperson
andgroup
after you add them to address book(and before you add them to group). ifrecordsIds
are not updated then that is the cause, and you will need to fetchrecord
for newrecordId
. Also check whether the newperson
andgroup
are being added to addresbook or not.*Currently, I do not mac access to verify my suggestion/answer.