executeFetchRequest耗时太长。

时间:2014-04-17 03:39:01

标签: ios core-data nsfetchrequest

即使我的桌子上没有超过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);

2 个答案:

答案 0 :(得分:0)

尝试替换此代码:

if (!fetchError) {
    for (NSManagedObject* obj in objects) {
        [loanIDArray addObject:[obj valueForKey:@"app_id"]];

    }
}

用这个来避免迭代:

if (!fetchError) {
    loanIDArray = [objects valueForKeyPath:@"app_id"];
}

答案 1 :(得分:0)

试试这个。

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"]];
    }
}