0

Working on someone else's code. I am really confused about whats exactly happening in this code.

ABAddressBookRef addressBook = ABAddressBookCreate(); 
ABRecordRef persons = ABAddressBookGetPersonWithRecordID(addressBook, x);
CFErrorRef *error=NULL;
ABAddressBookRemoveRecord(addressBook, persons, error);
ABAddressBookSave(addressBook, error);

ABRecordRef persons = ABPersonCreate(); 

ABRecordSetValue(persons, kABPersonFirstNameProperty, firstName , nil);

What should be done is that, a ABRecordRef person should be created. His properties should be set and his record/details should be displayed. He should never be saved in the address book. Is this the way to do it. Need help.

Edit: Apart from setting properties to person, the following code is added to push the view controller to view the person.

MyContactDetailViewcontroller *personContactDetail = [[MyContactDetailViewcontroller alloc] init];
personContactDetail.displayedPerson = persons; 
personContactDetail.passedSelectedContactData = selectedContactsOnlyData;
[self.navigationController pushViewController:personContactDetail animated:YES];
[personContactDetail release];

MyContactDetailViewcontroller subclass ABPersonViewController. Or is it more apt to use ABUnknownPersonViewController.

Do you find any cases where the contacts could be duplicated in address book

Xavi Valero
  • 2,047
  • 7
  • 42
  • 80

1 Answers1

1

This is exactly what is happening here.. I will explain..

ABAddressBookCreate creates a new address book from data from the addressbook database, so any changes you make to ABAddressBookRef will only be saved to actual addressbook database once you call ABAddressBookSave(). so what it is doing is getting the reference for the person with recordid- x. Than you are creating a new person entry using

ABRecordRef persons = ABPersonCreate(); 

and than you are setting its value, but this is available to this particular object and not stored in the database unless you call... ABAddressBookSave()

hoping this helps you... :)

Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
  • When ABAddressBookSave() is called, the person is actually being saved into the address book and any changes to its properties will also be reflected in the address book. Is that so? – Xavi Valero Apr 30 '12 at 16:25
  • ABAddressBookSave() is not called after the person was created so NO. If you call ABAddressBookSave() again than YES(only the changes you have made till that point). – Ankit Srivastava Apr 30 '12 at 16:29
  • `ABAddressBookSave(addressBook, error);`. What does this piece of code do. Does it save a person without any properties? – Xavi Valero Apr 30 '12 at 16:34
  • The first parameter specifies which instance do you want to replicate in to your address book database where as the second denotes any error that were generated will saving to the database... for more info refer this... http://developer.apple.com/library/ios/#DOCUMENTATION/AddressBook/Reference/ABAddressBookRef_iPhoneOS/Reference/reference.html – Ankit Srivastava Apr 30 '12 at 16:38
  • The issue I am facing is that, some contacts are duplicated in the address book. I am not sure whether the duplication is taking place in the above piece of code. As far as the above code is concerned, it shouldn't save any contacts in the addressbook. – Xavi Valero Apr 30 '12 at 16:41