2012-11-01 55 views
0
  • 我試圖將所有聯繫人備份到服務器上。將一個nsmutable數組的所有對象與另一個nsmutable數組的所有對象進行比較,並將新對象轉換爲另一個nsarray

  • 所以我轉換聯繫人到電子名片串併發送
    電子名片串到服務器,並存儲與 的recordId電子名片串到SQLite數據庫

  • 所以對於當用戶想在第一時間收回所有的 聯繫人將去到服務器,並在數據庫中。

  • 從第二次我只想發送新聯繫人到 服務器。所以,我需要新的 RecordIds比較存儲 與電話簿RecordIds的NSArray的數據庫RecordIds並獲得到一個數組

  • 比如我有一個像40,41,42,43,44,45 RecordIds到 dbarray。不一定按順序排列。

  • 現在,當用戶增加5組新的聯繫人的電話簿陣列將包含類似40,41,42,43,44,45,46,47,48,50

  • recordIds所以這次我只是想與recordIds 46,47,48,49和50.Something聯繫人發送類似的增量備份

  • 也是我需要的是檢查是否有存儲vCards.So我還需要比較所有的 任何修改來自 電話簿陣列和數據庫陣列的vCard字符串。

    我是新來的iOS編程,我卡住了。請 幫助!!!其緊急

回答

0

我通過使用下面的代碼解決了上述問題。希望它有助於某人。

-(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 
} 

}

相關問題