无法处理executeFetchRequest错误

时间:2012-03-01 19:25:18

标签: ios core-data nsmanagedobjectcontext nsfetchrequest

我的获取请求工作正常,我得到了我的获取对象没有任何问题。我想做的是,在实体不存在的情况下处理错误。问题是,我无法处理错误,因为当我调用executeFetechRequest时应用程序崩溃:错误:没有任何警告。

我的提取看起来像:

NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"Info" inManagedObjectContext:context];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"infoID" ascending:YES]];
[request setReturnsObjectsAsFaults:NO];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"infoID like %@",[a substringFromIndex:13]];
request.predicate = predicate;
request.fetchBatchSize = 1;

NSError *error = nil;

NSArray *results = [context executeFetchRequest:request error:&error];

if (error == nil) {
    ...
}
else {
    //handle error
}

正如我所说,只要实体存在就没有问题,但是如果它不存在我想要处理错误。任何的想法?干杯

1 个答案:

答案 0 :(得分:1)

您可以询问模型是否存在此类实体:

    NSArray *entities = managedObjectModel.entities;
    BOOL canExecute=NO;
    for(NSEntityDescription *ed in entities) {
       // check if entity name is equal to the one you are looking for
       if(found) {
          canExecute=YES;
          break;
       }
    }

   if(canExecute) {
     // execute your request and all the rest...
   } else {
     NSLog(@"Entity description not found");
   }

如果不存在,则不执行fetch reuest