我根据时间概要模板分析了我的应用程序,并注意到executeFetchRequest:error:执行的处理过程花费了大量时间,因为它多次完成,而且在coredata中有很多记录。
-(Customer *) getCustomerByName:(NSString *)_name
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like %@",_name];
NSFetchRequest *fetchReq = [[NSFetchRequest alloc] init];
[fetchReq setPredicate:predicate];
[fetchReq setEntity:[NSEntityDescription entityForName:@"customer" inManagedObjectContext:self.managedObjectContext]];
NSArray *all = [self.managedObjectContext executeFetchRequest:fetchReq error:nil];
if ([all count] > 0) {
return (Customer*)[all objectAtIndex:0];
}
return nil;}
以上方法的调用不止一次,我可以确认它正在执行大约2000次调用。因此,每次executeFetchRequest:error:都会在coredata中获取大量记录,以检查客户是否存在。为了减少所消耗的时间,是否需要更改executeFetchRequest:error:的处理?有什么想法吗?
谢谢。
发布于 2013-09-16 23:11:50
如果您将fetchLimit设置为1,它是否有帮助,似乎您只需要第一个结果。
[fetchReq setFetchLimit:1];https://stackoverflow.com/questions/18838616
复制相似问题