首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法处理executeFetchRequest错误

无法处理executeFetchRequest错误
EN

Stack Overflow用户
提问于 2012-03-02 03:25:18
回答 1查看 2.1K关注 0票数 1

我的fetch请求运行良好,并且我没有任何问题地获得我的fetch对象。我想做的是,在实体不存在的情况下处理错误。问题是,我无法处理这个错误,因为当我在没有任何警告的情况下调用executeFetechRequest: error:时,应用程序会崩溃。

我的fetch看起来像这样:

代码语言:javascript
复制
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
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-02 19:51:38

您可以询问模型是否存在这样的实体:

代码语言:javascript
复制
    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请求

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9522599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档