我已經複製了所有本地聯繫人並將它們存儲在覈心數據中,現在,當我使用時間分析器工具時,它顯示我正在花費大量時間從我的應用中刪除本地聯繫人。可以提供任何人我使用任何優化技術來改善我的代碼和應用程序性能。優化聯繫人刪除
這是我從核心數據的聯繫人刪除代碼:
+(void)deleteContacts
{
[[LoadingIndicator currentIndicator]displayActivity:@"Deleting Old contacts"];
//fetch the data from core data and delete them because you are going to sync again
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSManagedObjectContext *managedObjectContext=[appDelegate managedObjectContext];
fetchRequest.entity = [NSEntityDescription entityForName:@"PersonEvent" inManagedObjectContext:managedObjectContext];
NSInteger *contactsIdentifier=1;
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"sourceflag == %d", contactsIdentifier];
NSArray * persons = [managedObjectContext executeFetchRequest:fetchRequest error:nil];
//error handling goes here
if (persons.count>0) {
for (NSManagedObject * person in persons) {
[managedObjectContext deleteObject:person];
}
}
NSError *saveError = nil;
[managedObjectContext save:&saveError];
[[LoadingIndicator currentIndicator]displayCompleted:@"Done"];
[[LoadingIndicator currentIndicator]hide];
NSLog(@"Finished deleting local contacts..");
}
的時間探查表明了我94.3%的時間都花在 [managedObjectContext節省:& saveError]。
任何幫助,將不勝感激
感謝
+1因爲問題有關於應用性能的問題。 –
'NSInteger * contactsIdentifier = 1;'看起來很可疑。它是一個指向NSInteger的指針,但你可能想要使用NSInteger本身。 – eofster