나는이 단체가 있습니다코코아 : 어떻게 핵심 데이터 중복 관계를 피하기 위해?
- ProductsEntity을
- 이름
- 주문 [ProductsOrderRelationship]
- OrderEntity
- 제품 [ProductsOrderRelationship]
- ProductsOrderRelationship
- 위해 [OrderEntity]
- 제품 [ProductsEntity]
- 수량
는 지금, 나는 기존의 순서를 편집 할. 사용 가능한 제품 및 장바구니 목록이 있습니다.
이제 이러한 사용 가능한 제품을 장바구니에 추가하고 싶습니다.
코드는 제품이 존재하는지 확인해야하므로 수량이 증가합니다.
그러나, 지금, 단순히 ... 더 관계를 추가
나 코드의 조각을 공유하자! 인터페이스에는 왼쪽에 사용 가능한 제품 목록이 있고 오른쪽에는 장바구니 (주문 엔터티) 목록이 있습니다. 둘 다 배열 컨트롤러가 내 코드에 연결되어 있습니다. 그런 다음이 작업을 수행합니다.
- (IBAction)addSelectedProducts:(id)sender {
NSArray *firstSelectedProducts = [availableProductsController selectedObjects];
//Objects selected in the array controller
NSMutableArray *selectedProducts = [[NSMutableArray alloc] initWithCapacity:1];
//Here I will filter the repeated ones
NSMutableSet *newProducts = [NSMutableSet set];
//This is the final value to change in the current order entry.
NSMutableSet *oldProducts = [orderManagedObject valueForKey:@"products"];
//This is the old value I will change.
//Here we filter every repeated entries:
if ([firstSelectedProducts count] > 0) {
for (id object in firstSelectedProducts) {
if (![oldProducts containsObject:object]) {
[selectedProducts addObject:object];
}
}
}
//Here we create objects in the relationship entity:
for (int i = 0; i < [selectedProducts count]; i++) {
// Create new relationship.
NSManagedObject *newProductObject = [
NSEntityDescription
insertNewObjectForEntityForName:@"ProductsOrderRelationship"
inManagedObjectContext:managedObjectContext
];
[newProductObject setValue:[selectedProducts objectAtIndex:i] forKey:@"product"];
[newProductObject setValue:orderManagedObject forKey:@"order"];
[newProducts addObject:newProductObject];
[newProductObject release];
}
[newProducts unionSet:oldProducts];
//Join old products and new products.
[orderManagedObject setValue:newProducts forKey:@"products"];
//Re-set the value.
//(... release stuff here)
}
이 특정 문제에 대한 안내를 찾을 수 없습니다. 제안 사항이 있습니까?
피 중복 관계를 (당신이. 논리가 명확하게 수있는 클래스와 그 관련 변수의 이름을 변경. ProductsOrderRelationship 종종 LineItem을라고 부르는) 또는 데이터 항목을 중복? –