0

我需要将 25000 个联系人插入 iPhone 联系人,就我编写的代码而言,它在模拟器上运行良好,这可以在几分钟内实现。但是当我在 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);
}

提前致谢....

4

1 回答 1

1

首先使用快速枚举。

然后批量保存联系人,并在批量结束时将计数器编号保存到用户默认值或任何位置。这将阻止您再次重新开始应对过程。

于 2013-01-16T13:00:23.710 回答