1

我想将 ABRecordRef 添加到现有组。有些如何它不起作用。这是我的代码:

-(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);
}

每当我使用上述代码并尝试将其保存在现有组中时,我都会在控制台中收到以下消息

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 (?, ?, ?);

我没有得到上面代码中缺少的东西。感谢任何帮助。谢谢

4

1 回答 1

3

如果我们看到消息:

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 (?, ?, ?);

同样在文档中:

ABAddressBook 不透明类型(其实例称为地址簿)为地址簿提供了一个编程接口 - 一个由多个应用程序用来存储有关人员的个人信息的集中式数据库

(所以,在内部,这个错误是由于不正确的 sqlite 查询引起的。)

它表明ABGroupAddMemberorABAddressBookAddRecord(addressBookRef, group, &error);呼叫存在一些问题。

问题可能是:当我们调用ABAddressBookGetGroupWithRecordIDABAddressBookGetGroupWithRecordID函数时,我们已经有了组和个人引用。

现在,当ABAddressBookAddRecord为 both(persongroup) 执行时,它会将它们添加到addressbook不更新它们的 UniqueIds ( id for person and group ref.),因此在ABGroupAddMember函数调用时它具有person并且group具有相同的 id,这些 id 已添加到 ABGroupMembers表中。

请尝试在将它们添加到通讯簿之前和之后打印(以及在将它们添加到组之前recordIdpersongroup如果recordsIds没有更新,那就是原因,您需要获取recordnew recordId。还要检查新的persongroup是否被添加到地址簿。

*目前,我没有访问权限来验证我的建议/答案。

于 2012-04-27T18:37:58.297 回答