관리 대상 개체 컨텍스트의 변경 내용을 저장할 때 오류가 발생하지만 오류 처리기에 문제가 있습니다. 오류는 nil이므로 나에게 제공됩니다. 유용한 정보가 없습니다. 두 가지 버전의 오류 처리기가 있습니다. 이 사람은 엑스 코드에 의해 생성하고 작동했다 (즉, 로그 메시지가 유용한 오류 정보를 포함) :핵심 데이터 저장 관리 대상 개체 컨텍스트 - 오류 포인터가 없음
AppDelegate.c
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil)
{
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
{
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
을하지만 결국 (성공/실패를 통과 할 수 있도록하려면, 지금은 그냥 중단하고있다.) + 에러 정보를 호출자에게 되돌려 보낸다. 그래서 나는 작동하지 않는다. (에러는 무효이므로 에러에 대한 유용한 정보는 제공하지 않는다.)
Database.h
+ (BOOL) commit:(NSError **)error;
Database.c
+ (BOOL) commit:(NSError **)error {
AppDelegate *appDelegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
if (managedObjectContext != nil)
{
if ([managedObjectContext hasChanges] && ![managedObjectContext save:error])
{
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
*/
if (error == nil) {
NSLog(@"Unresolved error");
abort();
} else {
NSLog(@"Unresolved error %@, %@", *error, [*error userInfo]);
abort();
//return FALSE;
}
}
return TRUE;
}
return FALSE;
}
내 문제는 포인터이며, 리디렉션의 층에서 길을 잃지 확신 해요.
[편집 :] 호출하는 코드가 커밋 :
[Database commit:nil];
나는 commit 메소드의 시작과 같은 무언가를 추가 할 필요가 궁금,하지만 난 포인터에 대해 확실하지 않다 :
if (error == nil) {
error = [[NSError alloc] init];
}
1) 저장 작업이 실제로 실패합니까? 성공하면'error'는 아무것도 설정되지 않습니다. - 2)'commit :'메서드가 어떻게 호출되는지 알려줄 수 있습니까? - 3) 위의 코드는 오류 상황에서 중단되므로 오류를 반환 할 수 없습니다. –
@MartinR 1) 네, 실패합니다. 2) 커밋은 아무 것도하지 않고 통과하는 것입니다 ... 흠 ... 이것이 문제인지 궁금합니다. 3) 코드가 (지금은) 중단되지만 오류도 기록됩니다. 내 문제는 오류 = nil (또한 디버거를 통해 오류 = nil 확인) 때문에 두 번째 것 (오류 정보 포함) 대신 첫 번째 오류 (단지 "확인되지 않은 오류")입니다. – Sasha