저장하는 동안 iphone Core Data Unresolved 오류
저장하려고 할 때 코어 데이터에서 이상한 오류 메시지가 표시되지만 오류를 재현 할 수 없다는 문제 (다른 작업을 수행 할 때 다른 시간에 나타남)
오류 메시지 :
Unresolved error Domain=NSCocoaErrorDomain Code=1560 UserInfo=0x14f5480 "Operation could not be completed. (Cocoa error 1560.)", {
NSDetailedErrors = (
Error Domain=NSCocoaErrorDomain Code=1570 UserInfo=0x5406d70 "Operation could not be completed. (Cocoa error 1570.)",
Error Domain=NSCocoaErrorDomain Code=1570 UserInfo=0x14f9be0 "Operation could not be completed. (Cocoa error 1570.)"
);
}
오류를 생성하는 방법은 다음과 같습니다.
- (IBAction)saveAction:(id)sender {
NSError *error;
if (![[self managedObjectContext] save:&error]) {
// Handle error
NSLog(@"Unresolved error %@, %@, %@", error, [error userInfo],[error localizedDescription]);
exit(-1); // Fail
}
}
이 메시지의 이유에 대한 아이디어? 그것은 임의의 시간에 나타납니다
필수 속성이 없음을 의미합니다. * .xcodatamodel에서 "선택적"상자를 선택하거나 managedObjectContext에 저장할 때 특성이 채워져 있는지 확인하십시오.
두 가지 요구 사항에 맞게 코드를 변경 한 후 추가 오류가 발생하면 빌드를 정리하고 iPhone Simulator / iPhone 장치에서 응용 프로그램을 삭제하십시오. 모델 변경이 이전 모델 구현과 충돌 할 수 있습니다.
편집하다:
코어 데이터가 뱉어내는 모든 오류 코드는 거의 잊어 버렸습니다. 코어 데이터 상수 참조 이전에 문제가 있었으며 올바른 옵션 상자를 선택 취소했습니다. 문제를 찾는 데 어려움이 있습니다. 행운을 빕니다.
나는 잠시 동안 이것으로 고투했다. 여기서 진짜 문제는 당신이 가진 디버깅이 문제가 무엇인지 보여주지 않는다는 것입니다. 그 이유는 CoreData가 하나 이상의 문제가있는 경우 반환하는 "최상위 수준"NSError 객체에 NSError 객체 배열을 배치하기 때문입니다 (이로 인해 여러 문제를 나타내는 오류 1560 및 오류 배열이 표시됨) 1570 년대). CoreData에는 더 유용한 정보를 제공하는 문제가있는 경우 반환되는 오류에 정보를 숨기는 데 사용하는 소수의 키가있는 것 같습니다 (오류가 발생한 엔티티, 누락 된 관계 / 속성 등). ). userInfo 사전을 검사하는 데 사용하는 키 는 여기의 참조 문서에서 찾을 수 있습니다 .
이것은 저장하는 동안 반환 된 오류에서 합리적인 출력을 얻는 데 사용하는 코드 블록입니다.
NSError* error;
if(![[survey managedObjectContext] save:&error]) {
NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors != nil && [detailedErrors count] > 0) {
for(NSError* detailedError in detailedErrors) {
NSLog(@" DetailedError: %@", [detailedError userInfo]);
}
}
else {
NSLog(@" %@", [error userInfo]);
}
}
누락 된 필드를 알려주는 출력을 생성하여 문제를 훨씬 쉽게 해결할 수 있습니다.
나는 이것이 Charles의 발췌 문장에 더 많은 장식이긴하지만 대답으로 던지고 있습니다. NSLog의 직선 출력은 읽고 해석하기가 어려울 수 있으므로 공백을 던져 중요한 'userInfo'키의 값을 불러내는 것을 좋아합니다.
내가 사용한 방법의 버전이 있습니다. ( '_sharedManagedObjectContext'는 '[[[UIApplication sharedApplication] delegate] managedObjectContext]'에 대한 #define입니다.)
- (BOOL)saveData {
NSError *error;
if (![_sharedManagedObjectContext save:&error]) {
// If Cocoa generated the error...
if ([[error domain] isEqualToString:@"NSCocoaErrorDomain"]) {
// ...check whether there's an NSDetailedErrors array
NSDictionary *userInfo = [error userInfo];
if ([userInfo valueForKey:@"NSDetailedErrors"] != nil) {
// ...and loop through the array, if so.
NSArray *errors = [userInfo valueForKey:@"NSDetailedErrors"];
for (NSError *anError in errors) {
NSDictionary *subUserInfo = [anError userInfo];
subUserInfo = [anError userInfo];
// Granted, this indents the NSValidation keys rather a lot
// ...but it's a small loss to keep the code more readable.
NSLog(@"Core Data Save Error\n\n \
NSValidationErrorKey\n%@\n\n \
NSValidationErrorPredicate\n%@\n\n \
NSValidationErrorObject\n%@\n\n \
NSLocalizedDescription\n%@",
[subUserInfo valueForKey:@"NSValidationErrorKey"],
[subUserInfo valueForKey:@"NSValidationErrorPredicate"],
[subUserInfo valueForKey:@"NSValidationErrorObject"],
[subUserInfo valueForKey:@"NSLocalizedDescription"]);
}
}
// If there was no NSDetailedErrors array, print values directly
// from the top-level userInfo object. (Hint: all of these keys
// will have null values when you've got multiple errors sitting
// behind the NSDetailedErrors key.
else {
NSLog(@"Core Data Save Error\n\n \
NSValidationErrorKey\n%@\n\n \
NSValidationErrorPredicate\n%@\n\n \
NSValidationErrorObject\n%@\n\n \
NSLocalizedDescription\n%@",
[userInfo valueForKey:@"NSValidationErrorKey"],
[userInfo valueForKey:@"NSValidationErrorPredicate"],
[userInfo valueForKey:@"NSValidationErrorObject"],
[userInfo valueForKey:@"NSLocalizedDescription"]);
}
}
// Handle mine--or 3rd party-generated--errors
else {
NSLog(@"Custom Error: %@", [error localizedDescription]);
}
return NO;
}
return YES;
}
This allows me to see the value for 'NSValidationErrorKey', which, when I encountered the issue from the OP, pointed directly to the non-optional Core Data entities that I'd forgot to set before trying to save.
The problem touched me, when I save second record to CoreData. All not optional fields (relationship) was filled without nil as well, but in the error output I'd notice, that one of fields in first saved object had became nil. Strange a little? But the reason is quite trivial - one to one relationship which nullify first object, when I set it in the second.
So, the scheme is:
"Parent" with relationship "child" One to One
Create Child 1, set parent. Save - OK
Create Child 2, set parent. Save - Error, Child 1.Parent == nil
(behind the scene child 2 did nullify child 1 parent)
Changing the relationship in Parent from One to One to Many to One solved this task.
I had transient property of type int that wasn't optional. Obviously, when it was set to 0, 1570 error appear. Just changed all my transient properties to optional. Nil-check logic can be implemented in code if necessary.
I means that your model failed to validate, which could happen for a number of reasons: unused property in your model, missing value that's marked as required. To get a better understanding of what exactly went wrong, put a breakpoint in a place where you are ready to save your object, and call one of the validateFor...
method variants, like:
po [myObject validateForInsert]
More detailed information about the problem is in error description. Successful validation means you will get no output.
It helped me. Check this one too.
Check the optional box in your *.xcodatamodel objects
참고URL : https://stackoverflow.com/questions/1283960/iphone-core-data-unresolved-error-while-saving
'development' 카테고리의 다른 글
"느슨한 커플 링"이란 무엇입니까? (0) | 2020.05.26 |
---|---|
IntelliJ에서 코드 글꼴 크기를 늘리는 방법은 무엇입니까? (0) | 2020.05.26 |
5 초마다 페이지를 다시로드하는 방법? (0) | 2020.05.26 |
셀레늄 C # 웹 드라이버 : 요소가 나타날 때까지 기다립니다 (0) | 2020.05.26 |
Java "이 언어 레벨에서 지원되지 않는 람다 표현식" (0) | 2020.05.26 |