我在我的應用程序如下關係:增量核心數據屬性
產品 - >> ProductOrder < < - 訂單
然後我有兩個對象 - C三類,以etablish這些關係:
ProductOrder +產品
+ (ProductOrder *)addProductToOrderWithProduct:(Product *)product inManagedObjectContext:(NSManagedObjectContext *)context {
ProductOrder *orderProduct = nil;
orderProduct = [NSEntityDescription insertNewObjectForEntityForName:@"ProductOrder" inManagedObjectContext:context];
NSManagedObjectID *productID = [product objectID];
orderProduct.qnty = product.qnty;
orderProduct.price = product.price;
[(Product *)[context objectWithID:productID] addOrderedProductsObject:orderProduct];
return orderProduct;
}
然後訂購+ ProductOrder
+ (Order *)addOrderedProductToOrderWithOrderedProduct:(ProductOrder *)orderedProduct inManagedObjectContext:(NSManagedObjectContext *)context {
Order *order = nil;
order = [NSEntityDescription insertNewObjectForEntityForName:@"Order" inManagedObjectContext:context];
NSManagedObjectID *orderedProductID = [orderedProduct objectID];
[order addOrderProductsObject:(ProductOrder *)[context objectWithID:orderedProductID]];
return order;
}
我將產品添加到這些關係,像這樣:
for (Product *prod in [self.fetchedResultsController fetchedObjects]) {
[[[DataManager sharedInstance] backgroundManagedObjectContext] performBlock:^{
ProductOrder *prodOrder = [ProductOrder addProductToOrderWithProduct:prod inManagedObjectContext:[[DataManager sharedInstance] backgroundManagedObjectContext]];
Order *order = [Order addOrderedProductToOrderWithOrderedProduct:prodOrder inManagedObjectContext:[[DataManager sharedInstance] backgroundManagedObjectContext]];
NSInteger amount = [order.orderNumber integerValue];
amount++;
order.orderNumber = [NSString stringWithFormat:@"Order %@", [NSNumber numberWithInteger:amount]];
[[DataManager sharedInstance] saveBackgroundContext];
[[DataManager sharedInstance] saveMasterContext];
}];
}
我現在想組這些產品在報頭。我在Order實體中有這個orderNumber屬性,我想爲添加到關係中的每一組產品增加。我將如何設法做到這一點?
視覺例如:
非常感謝你,將代碼是不同的,如果我使用的是FRC? :) – 7c9d6b001a87e497d6b96fbd4c6fdf