简体   繁体   中英

compare all the objects of one nsmutable array with all the objects of another nsmutable array and get new objects into another nsarray

  • I am trying to take the back up of all the contacts on to the server.

  • So I am converting the contacts into vCard strings and sending the
    vCard strings onto the server and also storing the vCard strings with RecordID into the sqlite database

  • So for the first time when user wants to take back up all the contacts will go to the server and also in the database.

  • From the second time I just want to send only the new contacts to the server. So I need to compare the database RecordIds stored into the nsarray with Phonebook RecordIds and get the new RecordIds into an array

  • For example I have RecordIds like 40,41,42,43,44,45 into the dbarray.Not necessarily in sequence or order.

  • Now when user added 5 new contacts phonebook array will contain recordIds like 40,41,42,43,44,45,46,47,48,50

  • So This time I just want to send contacts with recordIds 46,47,48,49 and 50.Something like incremental backup

  • Also what I need is to check if there is any modification in the stored vCards.So I also need to compare all the vCard strings from phonebook array and database array.

    I am new to iOS programming and I am stuck. Please help!!! Its urgent

I solved the above problem by using the following code.Hope it helps someone.

 -(void)newContactsAndContactsToUpdate
  {
    NSMutableArray *newContact = [[NSMutableArray alloc]init];
    NSMutableArray *newContactRecIds = [[NSMutableArray alloc]init];
    NSMutableArray *updateContactRecIds = [[NSMutableArray alloc]init];
    NSMutableArray *updateContactRecIds = [[NSMutableArray alloc]init];
    //dataArray contains all the contacts(converted into Vcard strings) in the phonebook
    //recordIDArray contains the all recordids in the phonebook
    //dbArraywithRID contains the recordids stored in database.
    //dbArraywithVcard contains  contacts(converted into Vcard strings)stored in database
    for(int i=0;i< dataArray.count;i++)
    {
    BOOL found = NO;
    int phoneBookRecId=[[recordIDArray objectAtIndex:i] intValue];


    NSString * currentPhVc=[dataArray objectAtIndex:i];

    for(int j=0;j< dbArraywithRID.count;j++)
    {
        int dbRecId=[[dbArraywithRID objectAtIndex:j] intValue];

        if(dbRecId == phoneBookRecId)
        {
            found =YES;

            NSString * currentDBVc=[dbArraywithVcard objectAtIndex:j];

            if(![currentDBVc isEqualToString:currentPhVc])
            {
                NSLog(@" db rec  =%@    -   %@   ",currentDBVc,currentPhVc );
                [updateContactRecIds addObject:[NSNumber numberWithInt:phoneBookRecId]];

                [newContact addObject:currentPhVc];
                [newContactRecIds addObject:[NSNumber numberWithInt:phoneBookRecId]];

            }
            break;
        }
    }
    if(!found)
    {
        [newContact addObject:currentPhVc];
        [newContactRecIds addObject:[NSNumber numberWithInt:phoneBookRecId]];

    }
}


if(newContact!=nil && [newContact count] > 0)
{
    //newContact array contains all the contacts(vcard strings) that are new

}
if(updateContactRecIds!=nil && [updateContactRecIds count] > 0)
{
    updateContactRecIds contains all the recordids which need to be updated
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM