更新:目前尋找到的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倍,不是一件好事,它會崩潰的應用程序。
感謝您的閱讀:)
注:它被認爲表現欠佳有未命名的參數在你的選擇。 – retainCount 2009-11-25 18:55:57
感謝您的評論,我仍然在學習。我在哪裏做這個? – iOSDevil 2009-11-26 11:49:31