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:
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 (?, ?, ?);
Also in documentation :
The ABAddressBook opaque type (whose instances are known as address books) provides a programming interface to the Address Book—a centralized database used by multiple applications to store personal information about people
(So, internally, this error is caused due to incorrect sqlite query.)
It indicates that there is some issue with ABGroupAddMember
or ABAddressBookAddRecord(addressBookRef, group, &error);
call.
The problem can be: We have group and person references already with us when we callABAddressBookGetGroupWithRecordID
and ABAddressBookGetGroupWithRecordID
functions.
Now, when ABAddressBookAddRecord
is executed for both( person
and group
) it is adding them to addressbook
but not updating their UniqueIds(id for person and group ref.
) so at the time of ABGroupAddMember
function call it has person
and group
with same ids which are added in ABGroupMembers
table.
Please, try to print recordId
for person
and group
after you add them to address book(and before you add them to group). if recordsIds
are not updated then that is the cause, and you will need to fetch record
for new recordId
. Also check whether the new person
and group
are being added to addresbook or not.
*Currently, I do not mac access to verify my suggestion/answer.