我的fetch请求运行良好,并且我没有任何问题地获得我的fetch对象。我想做的是,在实体不存在的情况下处理错误。问题是,我无法处理这个错误,因为当我在没有任何警告的情况下调用executeFetechRequest: error:时,应用程序会崩溃。
我的fetch看起来像这样:
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
}正如我所说的,只要实体存在就没有问题,但如果它不存在,我想要处理错误。有什么想法吗?干杯
发布于 2012-03-02 19:51:38
您可以询问模型是否存在这样的实体:
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请求
https://stackoverflow.com/questions/9522599
复制相似问题