RHAddressBook
RHAddressBook copied to clipboard
newPersonInDefaultSource retains emails and phone numbers
I am working on a App which Syncs new/existing contacts with the default Address book. To avoid duplicates I am searching for person in the AddressBook and if I get any results I simply update the Person. Furthermore, to avoid duplication of phone numbers and email addresses within the Person I do the following,
//Set phone number if exists if(user.phone){ RHMultiStringValue *phoneMultiValue = [person phoneNumbers]; RHMutableMultiStringValue *mutablePhoneMultiValue = [phoneMultiValue mutableCopy]; if (! mutablePhoneMultiValue) mutablePhoneMultiValue = [[RHMutableMultiStringValue alloc] initWithType:kABMultiStringPropertyType];
//Avoid Duplicates
NSInteger phoneIndex = [mutablePhoneMultiValue firstIndexOfValue:user.phone];
if(phoneIndex == -1){
[mutablePhoneMultiValue addValue:user.phone withLabel:RHPersonPhoneIPhoneLabel];
person.phoneNumbers = mutablePhoneMultiValue;
}
}
//Set email if exists
if( user.email){
RHMultiStringValue *emailMultiValue = [person emails];
RHMutableMultiStringValue *mutableEmailMultiValue = [emailMultiValue mutableCopy];
if (! mutableEmailMultiValue) mutableEmailMultiValue = [[RHMutableMultiStringValue alloc] initWithType:kABMultiStringPropertyType];
//Avoid duplicates
NSInteger emailIndex = [mutableEmailMultiValue firstIndexOfValue:user.email];
if(emailIndex == -1){
[mutableEmailMultiValue addValue:user.email withLabel:RHWorkLabel];
person.emails = mutableEmailMultiValue;
}
}
This works great for users that are already in the AddressBook but when I created a new user with, [RHPerson newPersonInSource:[self.addressBook defaultSource]];
Things get funky!! [person emails] and [person phoneNumbers] returns an array with pre-populated values from random contacts in the AddressBook even though a new RHPerson was created.
I did manage to workaround it forcing initialization on setting emails and phoneNumbers when a new Person is created as follows,
person.emails = [[RHMultiStringValue alloc] init]; person.phoneNumbers = [[RHMultiStringValue alloc] init];
This fixed my issue but would like to dig deep as to why this is happening.
No idea. Possibly because the created objects are not saved to the store. Feel free to upload a sample project or write a unit-test to exercise the issues you are having and i'll see what i can ascertain.