0

I need to insert 25000 contacts to iPhone contacts as far as I coded it works fine on simulator and this can be achieved in few minutes. But when I import this on a iPhone 4s it takes more than 3 hours and also only partial contacts gets saved and then app gets closed.

Help me with a easy way to insert multiple records to address book...

Here is my piece of code...

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

Thanks in advance....

Arvind
  • 493
  • 1
  • 8
  • 23

1 Answers1

1

First of all use fast enumeration.

Then save contact in batches and at the end of a batch save the counter number to userdefaults or anywhere. This will prevent you to again re-starting the coping process.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140