我已经复制了所有本地联系人并将它们存储在核心数据中,现在当我使用时间分析器工具时,它显示我花费了大量时间从我的应用程序中删除本地联系人。任何人都可以建议我任何优化技术来改进我的代码和应用程序性能。
这是我从核心数据中删除联系人的代码:
+(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 save:&saveError];
任何帮助,将不胜感激
谢谢