即使我的表中没有超过3条记录,也只有一个表。方法: executeFetchRequest耗时太长
loanIDArray=[[NSMutableArray alloc]init];
appDelegate=[[UIApplication sharedApplication] delegate];
context=[appDelegate managedObjectContext];
entityDesc=[NSEntityDescription entityForName:@"Personal_Info" inManagedObjectContext:context];
NSFetchRequest* request=[[NSFetchRequest alloc]init];
[request setEntity:entityDesc];
NSPredicate* predicate=[NSPredicate predicateWithFormat:@"status_of_form==%@",@"Completed"];
[request setPredicate:predicate];
NSError* fetchError;
NSError* error;这条线太长了
NSArray* objects = [context executeFetchRequest:request error:&fetchError];
if (!fetchError) {
for (NSManagedObject* obj in objects) {
[loanIDArray addObject:[obj valueForKey:@"app_id"]];
}
}
else
NSLog(@"Status Fetch Error : %@",fetchError.description);
[context save:&error];
if (!error)
NSLog(@"count : %d",loanIDArray.count );
else
NSLog(@"error : %@",error.description);发布于 2014-04-17 13:02:14
尝试替换此代码:
if (!fetchError) {
for (NSManagedObject* obj in objects) {
[loanIDArray addObject:[obj valueForKey:@"app_id"]];
}
}这样可以避免迭代:
if (!fetchError) {
loanIDArray = [objects valueForKeyPath:@"app_id"];
}发布于 2014-04-17 13:04:59
尝尝这个。
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
[fetchRequest setEntity:entityDesc];
NSError *error = nil;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status_of_form==%@",@"Completed"];
[fetchRequest setPredicate:predicate];
NSArray *request = [context executeFetchRequest:fetchRequest error:&error];
if (!request.count==0) {
for (int i = 0; i<=request.count-1;i++) {
NSManagedObject *obj = [request objectAtIndex:i];
[loanIDArray addObject:[obj valueForKey:@"app_id"]];
}
}https://stackoverflow.com/questions/23124171
复制相似问题