如何在ABAddressbook中插入多个联系人?

时间:2013-01-16 12:56:49

标签: iphone ios contacts abaddressbook

我需要向iPhone联系人插入25000个联系人,只要我编码它在模拟器上工作正常,这可以在几分钟内实现。但是当我在iPhone 4s上导入它时,它需要超过3个小时,并且只保留部分联系人,然后应用程序关闭。

帮我一个简单的方法,将多个记录插入地址簿......

这是我的代码......

for (int i = 0; i < [contactNameArray count]; i++) {
    //Create new person and save to this group
    ABRecordRef record = ABPersonCreate();
    BOOL isSuccess ;
    NSString *firstname = [NSString stringWithFormat:@"%@",[contactNameArray objectAtIndex:i]];
    isSuccess  = ABRecordSetValue(record, kABPersonFirstNameProperty,(__bridge CFStringRef)firstname, &error); 
    isSuccess  = ABRecordSetValue(record, kABPersonLastNameProperty,CFSTR("Custom Contacts"), &error); 

    ABMutableMultiValueRef copyOfPhones = ABMultiValueCreateMutable(kABPersonPhoneProperty);
    NSString *phonenumber = [NSString stringWithFormat:@"%@",[contactPhoneArray objectAtIndex:i]];
    CFTypeRef phone= (__bridge CFStringRef)phonenumber;
    ABMultiValueAddValueAndLabel(copyOfPhones, phone,kABPersonPhoneMobileLabel,NULL);
    isSuccess = ABRecordSetValue(record, kABPersonPhoneProperty, copyOfPhones, &error);
    CFRelease(copyOfPhones);

    ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    NSString *emailid = [NSString stringWithFormat:@"%@",[contactEmailArray objectAtIndex:i]];
    CFTypeRef email= (__bridge CFStringRef)emailid;
    ABMultiValueAddValueAndLabel(multiEmail, email, kABWorkLabel, NULL);
    ABRecordSetValue(record, kABPersonEmailProperty, multiEmail, &error);
    CFRelease(multiEmail);

    isSuccess = ABAddressBookAddRecord(ab, record, &error);
    isSuccess = ABAddressBookSave(ab, &error);

    ABGroupAddMember(group, record, &error);

    NSLog(@"is success %d", isSuccess);

    ABAddressBookSave(ab, &error);
}

提前致谢....

1 个答案:

答案 0 :(得分:1)

首先使用快速枚举。

然后分批保存联系人,在批处理结束时将计数器号码保存到用户默认或任何地方。这将阻止您再次重新开始应对过程。