Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

newPersonInDefaultSource retains emails and phone numbers #41

Open
naqi opened this issue Feb 19, 2014 · 1 comment
Open

newPersonInDefaultSource retains emails and phone numbers #41

naqi opened this issue Feb 19, 2014 · 1 comment

Comments

@naqi
Copy link

naqi commented Feb 19, 2014

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.

@heardrwt
Copy link
Owner

heardrwt commented Mar 4, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants