2012-03-01 3 views
1

내 가져 오기 요청이 정상적으로 작동하며 가져온 개체를 아무런 문제없이 가져옵니다. 내가하고 싶은 것은 엔터티가 존재하지 않을 경우 오류를 처리하는 것입니다. 문제는, executeFetechRequest : error :를 호출 할 때 앱이 충돌하기 때문에 경고를 처리 할 수 ​​없다는 것입니다. executeFetchRequest 오류를 처리 할 수 ​​없습니다.

처럼 외모를 가져 내 :

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

실제로 좋은 질문입니다./... 다른 버전의 앱간에 동기화 할 때 확인해야한다고 생각했지만 사실이 사례를 다른 방식으로 잡습니다. 감사합니다 하하 ...하지만 엔티티가 존재하는지 확인하지 않아서 앱이 충돌하지 않는 이유는 100 % 확실합니다. – wolfrevo

+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"); 
    } 

당신이이 reuest

관련 문제