我已经复制了所有本地联系人并将它们存储在核心数据中,现在,当我使用时间分析器工具时,它显示我正在花费大量时间从我的应用中删除本地联系人。可以提供任何人我使用任何优化技术来改善我的代码和应用程序性能。优化联系人删除
这是我从核心数据的联系人删除代码:
+(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