2009-11-25 21 views
1

更新:目前尋找到的NSSet的minusSet 鏈接:Comparing Two Arrays批量更新和偶爾插入(coredata) - 太慢

嗨,大家好,

能否從這裏你的智慧中獲益..

我M在我的應用程序使用Coredata,第一次啓動我下載的數據文件,並插入了500個對象(每個60個屬性) - 快速,沒有任何問題。

每個隨後推出我下載的文件的更新版本,從中我需要更新現有的所有對象的屬性(可能除了5屬性)和已被添加到下載的文件的項目創建新的。

因此,首先推出的,我得到500個對象,..說一個星期後,我的文件現在包含507項..

我創建兩個數組,一個用於現有和一個用於下載。

NSArray *peopleArrayDownloaded = [CoreDataHelper getObjectsFromContext:@"person" :@"person_id" :YES :managedObjectContextPeopleTemp]; 
NSArray *peopleArrayExisting = [CoreDataHelper getObjectsFromContext:@"person" :@"person_id" :YES :managedObjectContextPeople]; 

如果每個陣列的數量等於話,我只是這樣做:

NSUInteger index = 0; 
if ([peopleArrayExisting count] == [peopleArrayDownloaded count]) { 
    NSLog(@"Number of people downloaded is same as the number of people existing"); 
    for (person *existingPerson in peopleArrayExisting) { 
     person *tempPerson = [peopleArrayDownloaded objectAtIndex:index]; 
     // NSLog(@"Updating id: %@ with id: %@",existingPerson.person_id,tempPerson.person_id); 
     // I have 60 attributes which I to update on each object, is there a quicker way other than overwriting existing? 
     index++; 
    } 
} else { 
    NSLog(@"Number of people downloaded is different to number of players existing"); 

所以現在來的慢的部分。

我最終使用的(這是tooooo慢):

  NSLog(@"Need people added to the league"); 
     for (person *tempPerson in peopeArrayDownloaded) { 
      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"person_id = %@",tempPerson.person_id]; 
      // NSLog(@"Searching for existing person, person_id: %@",existingPerson.person_id); 
      NSArray *filteredArray = [peopleArrayExisting filteredArrayUsingPredicate:predicate]; 
      if ([filteredArray count] == 0) { 
       NSLog(@"Couldn't find an existing person in the downloaded file. Adding.."); 
       person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"person" inManagedObjectContext:managedObjectContextPeople]; 

有沒有一種方法來生成的索引項新的陣列指的是在我的下載文件中的其他項目?

順便說一句,我的tableViews我使用NSFetchedResultsController所以更新的屬性將調用[細胞setNeedsDisplay]; ..每個細胞約60倍,不是一件好事,它會崩潰的應用程序。

感謝您的閱讀:)

+0

注:它被認爲表現欠佳有未命名的參數在你的選擇。 – retainCount 2009-11-25 18:55:57

+0

感謝您的評論,我仍然在學習。我在哪裏做這個? – iOSDevil 2009-11-26 11:49:31

回答

3

我會說,我還是新來使用Core Data框架,但我的猜測是,你的問題就出在for循環您發佈開始。

如果你看看你的循環中,每次執行它的時候創建一個新的NSPredicate對象,然後過濾現有的陣列尋找匹配。在一個小的數據集上,這種技術可以處理看起來很小的性能損失;但是,對於大數據集,您最終會花費大量時間創建僅在您提供的名稱上不同的NSPredicate對象。我建議你看看如何創建一個謂詞,然後使用變量替換來執行搜索。有關變量用於謂詞檢出的信息,請參見:http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CoreData/Articles/cdImporting.html#//apple_ref/doc/uid/TP40003174

作爲便箋,您可能還會考慮如何對數據進行排序以及如何執行搜索操作。另外我注意到的是,你不會釋放你的NSPredicate對象,所以你只是把內存扔掉了。