2013-09-29 5 views
0

내 coredata 관계 선박에서 항목을 가져 오는 방법을 알고 싶습니다. 나는 그것이 사전이나 배열 또는 뭔가를 반환 할 수 있도록 뭔가를해야 할 것 같아요.coredata 관계 가져 오기

나는이 기사를 읽거나 읽는 방법을 알고 있지만이 관계는 혼란 스럽다.

나는 sucsessfully coredata에 관계를 썼다고 생각하지만 지금은 내가 제대로 읽었는지 읽을 수 있기를 바란다. 나는 이것에 대한 방법을 쓰기 시작했으나 실제로 무엇을 해야할지 전혀 모른다. 모든 정보를 빼내는 것.

이있는 코드를 내가 가지고 지금까지 ..에 대한

- (void)writeFin:(NSArray *)recivedProjectData ItemsData:(NSArray *)itemsData { 
    // WRITE TO CORE DATA 
    NSManagedObjectContext *context = [self managedObjectContext]; 


    for (NSDictionary *dict in recivedProjectData) { 
     project = [NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:context]; 


     project.proNumber = [dict valueForKey:@"ProNumber"]; 
     project.projectDescription = [dict valueForKey:@"Description"]; 
//  project.items = [dict valueForKey:@""]; // this is the relationship for project 

    } 

    for (NSDictionary *dict in itemsData) { 
     items = [NSEntityDescription insertNewObjectForEntityForName:@"Items" inManagedObjectContext:context]; 


     items.Number = [dict valueForKey:@"Number"]; 
     items.Description = [dict valueForKey:@"Description"]; 
     items.comment = [dict valueForKey:@"Comment"]; 
     items.project = project; // this is the relationship for items 

     [project addItemsObject:items]; 
    } 


    NSError *error = nil; 

    if (![__managedObjectContext save:&error]) { 
     NSLog(@"There was an error! %@", error); 
    } 
    else { 
     NSLog(@"created"); 
    } 
} 


- (NSMutableArray *)readFin { 
    NSManagedObjectContext *context = [self managedObjectContext]; 


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:context]; 
    [fetchRequest setEntity:entity]; 

    NSError *error; 

    NSMutableArray *projectDictionaryArray = [[NSMutableArray alloc] init]; 


    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; 
    for (ProjectList *projectList in fetchedObjects) { 

     NSMutableDictionary *tempProjectDictionaryArray = [[ NSMutableDictionary alloc] init]; 

     [tempProjectDictionaryArray setObject:project.proNumber forKey:@"ProNumber"]; 
     [tempProjectDictionaryArray setObject:project.description forKey:@"Description"]; 
     [tempProjectDictionaryArray setObject:project.projectID forKey:@"ProjectID"]; 

     [projectDictionaryArray addObject:tempProjectDictionaryArray]; 
    } 
    return projectDictionaryArray; 
} 

그러니 그냥 오 투자 의견, I는 알고 싶다, 내 쓰기 방법입니다

괜찮아 보여 읽기와 쓰기 모두? B, 핵심 데이터에서 관계 객체를 가져 오거나 읽는 방법은 무엇입니까?

모든 도움을 주시면 대단히 감사하겠습니다.

답변

1

코어 데이터의 관계는 막 다른 골목이 아닌 모델의 다른 객체와 일치하는 객체 인 객체가 아닙니다. 당신은 당신의 관계를 최대한 멀리 볼 수 확인 여부를 확인 거기까지 이미 대부분의 방법이야 - 당신이 당신의 projectList에

[tempProjectDictionaryArray setObject: project.items forKey:@"items"]; 

객체를 하나 더 줄을 추가하기 만하면 무엇을 당신을 NSSet이 추가 될 것입니다. 그런 다음 설정을 마친 후에 이런 일이 반복되는지 확인하십시오.

NSSet itemsForProject = projectDictionaryArray[someIndex][@"items"] 
for (Item* currItem in [itemsForProject allObjects]) { 

    //access some property of the current item to make sure you have the right ones -- \ 
    description for example 
    NSLog(@"%@", item.description); 
} 
+0

괜찮은지 확인하고 있습니다. 어쩌면 내가이 루프를 읽을 필요가 있다고 생각하지만 이제 NSSet이 언급 했으니 많은 분노를 느낄 것입니다. – HurkNburkS

+0

코드에서 업데이트가 완료되었습니다. – HurkNburkS