2010-06-26 8 views
4

핵심 데이터 iPhone 애플리케이션을 구축 중이며 일대 다 관계 데이터를 검색하는 데 문제가 있습니다. 설명하는 동안 나와 함께 견뎌주십시오.iPhone 핵심 데이터 관계 오류

데이터 모델 디자이너를 사용하여 "항목"이라는 엔티티를 설정하고 "주석"이라는 많은 엔티티가 포함되어 있습니다. 그런 다음 여러 엔터티를 검색하여 UITableView에 표시합니다. 행의 사용자 탭은 I가 특정 entiny을 선택하면

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Items" inManagedObjectContext:self.managedObjectContext]; 
[request setEntity:entity]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(Item_to_Areas.Name LIKE %@)",[areaManagedObject valueForKey:@"Name"]]; 
[request setPredicate:predicate]; 
[request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@"Item_to_item_comments"]]; 
NSLog(@"Results: %@", [mutableItemsFetchResults description]); 
mutableItemsFetchResults = [[managedObjectContext executeFetchRequest:request error:nil] mutableCopy]; 
[request release]; 

는, init 메소드의 새로운 테이블 뷰 제어기로 전달하고, 밀어 : I합니다 (viewDidLoad 방법) 이와 같은 이들 엔티티 페치 스택에 새로운 뷰 컨트롤러 : 제 1 블록의

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
NSLog(@"itemObject: %@", [mutableItemsFetchResults objectAtIndex:indexPath.row]); 
InspectionItemCommentsViewController *itemCommentsViewController =  [[InspectionItemCommentsViewController alloc] 
                   initWithManagedObjectContext:self.managedObjectContext 
                   itemObject:[mutableItemsFetchResults objectAtIndex:indexPath.row]]; 
itemCommentsViewController.hidesBottomBarWhenPushed = YES; 
[self.navigationController pushViewController:itemCommentsViewController animated:YES]; 
[itemCommentsViewController release]; 
} 

는 NSLog 출력은 "Item_to_item_comments"관계 엔티티 검색되었음을 보여 주지만 초 그것이라고 I는 [요청 setRelationshipKeyPathsForPrefetching 호출하지 않더라도 : [NSArray arrayWithObject : @ "Item_to_item_comments"]].

다음은 첫 번째 NSLog 출력의 일부입니다

Results: (
"<NSManagedObject: 0xb356b70> (entity: Items; id: 0xb34fe60 <x-coredata://E43A90B5-AF0E- 4394-B4A7-5EFE74E181F8/Items/p1> ; data: {\n Clean = nil;\n Description = \"\";\n ItemToInformation = \"<relationship fault: 0x5e1ef00 'ItemToInformation'>\";\n \"Item_to_Areas\" = \"0xb33fd30 <x-coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Areas/p1>\";\n \"Item_to_item_comments\" = \"<relationship fault: 0x5e1d300 'Item_to_item_comments'>\";\n Keys = nil;\n Name = Other;\n \"Tenant_agrees\" = nil;\n Undamaged = nil;\n Working = nil;\n})", 
"<NSManagedObject: 0xb35a930> (entity: Items; id: 0xb32acc0 <x-coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Items/p2> ; data: {\n Clean = nil;\n Description = \"\";\n ItemToInformation = \"<relationship fault: 0x790de40 'ItemToInformation'>\";\n \"Item_to_Areas\" = \"0xb33fd30 <x-coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Areas/p1>\";\n \"Item_to_item_comments\" =  (\n  \"0xb35bcb0 <x-coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Item_Comments/p13>\",\n  \"0xb35bcd0 <x-coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Item_Comments/p37>\",\n  \"0xb35bca0 <x-coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Item_Comments/p5>\",\n  \"0xb35bcc0 <x-coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Item_Comments/p26>\"\n );\n Keys = nil;\n Name = Lights;\n \"Tenant_agrees\" = nil;\n Undamaged = nil;\n Working = nil;\n})", 

당신은 항목 개체가 Item_to_item_comments을 포함하여 가져온 것을 볼 수 있습니다.

itemObject: <NSManagedObject: 0xe20af50> (entity: Items; id: 0xe209fc0 <x- coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Items/p2> ; data: { 
    Clean = nil; 
    Description = ""; 
    ItemToInformation = "<relationship fault: 0xe302e40 'ItemToInformation'>"; 
    "Item_to_Areas" = "0xe500b90 <x-coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Areas/p1>"; 
    "Item_to_item_comments" = "<relationship fault: 0xe302e60 'Item_to_item_comments'>"; 
    Keys = nil; 
    Name = Lights; 
    "Tenant_agrees" = nil; 
    Undamaged = nil; 
    Working = nil; 

}) 

이제 Item_to_item_comments가 잘못 : 여기에 두 번째 NSLog입니다. 마찬가지로 푸시 된보기 컨트롤러에서 Items 엔터티는 전달되지만 Item_to_item_comments은 전달되지 않습니다.

나는 분명히 뭔가를 놓치고 있다고 생각하지만, 하루가 지나면이 문제를 해결할 수 없다.

도움을 주시면 감사하겠습니다.

피터

답변

9

A "오류가"오류를 의미하는 것은 아니다, 그냥 반환 된 객체가 읽어 그 속성이없는 "유령"을 의미한다. 관계의 다른 측면 때문에 대한 오류를 얻을 정상입니다 그 관계를 통해 통제되지 않은 개체 생성 계단을 시작하기 위해 가져온 것을 가져 오지는 않습니다.

오류 속성에 액세스하면 오류가있는 모든 기능 개체로 오류가 발생합니다.

편집 : 코멘트에서

: 나는 NSLog를 통해 같은 관계를 요청하고,하지만 난 여전히 관계를 개체를 얻을 수 것을

문제입니다.

아니요. 너 자신은 엔터티 자체를 Items 요청한 다음 로깅합니다. 그들은 예상대로 관계가 잘못되었다고보고하고 있습니다. 관계의 다른면에있는 실제 객체에 대해 각각을 요구하는 경우에만 결함 대신 객체를 볼 수 있습니다.

NSLog(@"itemObject.Item_to_item_comments: %@", [mutableItemsFetchResults objectAtIndex:indexPath.row].Item_to_item_comments.someAttribute]); 

당신은 다른 문제는 두 개의 별도의 인출의 결과를 비교하는 것입니다 위치 :

이 당신이 관계에있는 개체의 오류있는 인을 강제해야 할 것입니다. 이 :

NSLog(@"Results: %@", [mutableItemsFetchResults description]); 
mutableItemsFetchResults = [[managedObjectContext executeFetchRequest:request error:nil] mutableCopy]; 

... mutableItemsFetchResults이전이 페치가 발생 기록합니다. 다음 NSLog (아마도)는 가져온 후 결과를 기록합니다. 즉, 서로 다른 두 가지 오류 상태에서 두 개의 서로 다른 객체 집합을 볼 수 있습니다.

mutableItemsFetchResults은 분명히 재산이기 때문에 표시 될 수 있지만 올바르게 보관되었는지는 self.mutableItemsFetchResults 표기법을 사용하고 있지 않기 때문에 문제가 발생할 수 있습니다. 또한 mutableCopy이 필요하다고 생각하지 않습니다.

+0

본인은 코어 데이터가 요청 될 때까지 "대다"관계를로드하지 않는다는 것을 알고 있습니다. 문제는 NSLog를 통해 그러한 관계를 요청하고 있지만 여전히 관계 엔티티를 얻을 수 없다는 것입니다. 이 문제에 대한 설명이 적절하기를 바랍니다. 다시 한 번 감사드립니다. – futureshocked

+0

확장을 보려면 내 편집을 참조하십시오. – TechZen

+0

감사합니다. 큰 도움이됩니다. 초보자 실수! – futureshocked