tags:

views:

14

answers:

1

Hi guys,

Here I had a problem that I am adding contact from the address book and checking it whether it is already in the favourites list or not.If not I am adding the contact to favourite list.

  - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
  {

ContactDTO* dtoObject = [[ContactDTO alloc] init];

ABRecordID personId = ABRecordGetRecordID(person);
NSString* personIdStr = [NSString stringWithFormat:@"%d", personId];
dtoObject.contactId = personIdStr;

NSString *lastNameString, *firstNameString;

firstNameString = [self getValueForProperty:kABPersonFirstNameProperty forContact:personIdStr];
lastNameString = [self getValueForProperty:kABPersonLastNameProperty forContact:personIdStr];

dtoObject.firstName =  firstNameString;
dtoObject.lastName =  lastNameString;

printf("\n *****************firstNameString %s",[firstNameString UTF8String]);
//ABMultiValueRef emailMultiValue =[(NSString *)ABRecordCopyValue(person, kABPersonEmailProperty) autorelease];

ABMultiValueRef phoneMultiValue =[(NSString *)ABRecordCopyValue(person, kABPersonPhoneProperty) autorelease];
if (ABMultiValueGetCount(phoneMultiValue) > 0)
{
    ABMultiValueRef phoneMultiValue =[(NSString *)ABRecordCopyValue(person, kABPersonPhoneProperty) autorelease];

    NSString* curentTypeLabel =[(NSString *)ABMultiValueCopyLabelAtIndex(phoneMultiValue,identifier) autorelease];
    curentTypeLabel = [curentTypeLabel stringByReplacingOccurrencesOfString:@"_$!<" withString:@""];
    curentTypeLabel = [curentTypeLabel stringByReplacingOccurrencesOfString:@">!$_" withString:@""];

    dtoObject.numberType = curentTypeLabel;

    NSString* currentPhone = [(NSString *)ABMultiValueCopyValueAtIndex(phoneMultiValue,identifier) autorelease];
    dtoObject.mobNumber = currentPhone; 

    FavoritesAppDelegate* appDelegate = (FavoritesAppDelegate*) [[UIApplication sharedApplication] delegate];

    if ([favoritesArray count] > 0)
    {
        for (int i=0; i< [favoritesArray count]; i++)
        {

            ContactDTO* dtoObject1 = [favoritesArray objectAtIndex:i];
            printf("\n dtoObject1.contactId value = %s,   Main value = %s",[dtoObject.firstName UTF8String],[dtoObject1.firstName UTF8String]);
            printf("\n dtoObject1.mobNumber value = %s,  Main mobNumber value = %s",[dtoObject1.mobNumber UTF8String],[dtoObject.mobNumber UTF8String]);
            if ([dtoObject.firstName isEqualToString:dtoObject1.firstName])
            {

                printf("\n inside if....");
            }
            else
            {
                [appDelegate addContactToFavorites:dtoObject];
                break;
                printf("\n inside else....");

            }

        }
    }
    else
    {
        [appDelegate addContactToFavorites:dtoObject];
    }

    [self dismissModalViewControllerAnimated:YES];

}
/*else if(ABMultiValueGetCount(emailMultiValue) > 0)
{
    NSString* currentEmail =(NSString *)ABMultiValueCopyValueAtIndex(emailMultiValue,identifier);
    printf("\n *************currentEmail** %s",[currentEmail UTF8String]);
    [self emailBtnAction:currentEmail];


}*/
[dtoObject release];

return NO;
 }

For that the code I written was shown as above. Eventhough I am getting the duplicate values the condition is failed and duplicate values are added. Can anyone give suggestions to get rid of this? Anyone's help will be much appreciated.

Thanks to all, Monish.

A: 

You are adding to favorites array if the new contact doesn't match ONE of the existing items in the favorites array. But the new contact could be in the favorites array further along in the array. You need to check if the new contact doesn't match ALL of the existing favorites.

Also, shouldn't the match condition be more than just checking first names?

Something like this:

BOOL contactAlreadyExists = NO;
for (int i=0; i< [favoritesArray count]; i++)
{
  ContactDTO* dtoObject1 = [favoritesArray objectAtIndex:i];
  if ([dtoObject.firstName isEqualToString:dtoObject1.firstName])
  {
    contactAlreadyExists = YES;
    break;
  }
}
if (!contactAlreadyExists)
{
  [appDelegate addContactToFavorites:dtoObject];
}
aBitObvious
can u please explain with editing the code I posted.
How can we check All contacts in an array?
Thanks alot its working fine coz of ur suggestion.You also saved my time.