本文介绍了在订购应用程序中设置Core数据关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我目前为我的订购应用程式设定了以下关系。 产品< - 购物车 这是正常工作,产品被添加到购物车等等。当用户按下购物车视图控制器中的订单按钮时,我想要所有这些产品,它们刚刚订购显示在我的其他视图控制器。我想显示用户做出的所有订单。但我不能真正了解如何设置我的Core数据模型,以实现这一目标?我需要一对一的关系从购物车或一对多的关系或完全不同的东西? 感谢您的帮助。 编辑1: 编辑2: code> +(ProductOrder *)addProductToOrderWithProduct:(Product *)productManagedObjectContext:(NSManagedObjectContext *)context { ProductOrder * orderProduct = nil; orderProduct = [NSEntityDescription insertNewObjectForEntityForName:@ProductOrderinManagedObjectContext:context]; NSManagedObjectID * productID = [product objectID]; orderProduct.antal = product.antal; [(Product *)[context objectWithID:productID] addOrderedProductsObject:orderProduct]; return orderProduct; } +(Order *)addOrderedProductToOrderWithOrderedProduct:(ProductOrder *)orderedProduct inManagedObjectContext:(NSManagedObjectContext *)context { Order * order = nil ; order = [NSEntityDescription insertNewObjectForEntityForName:@OrderinManagedObjectContext:context]; //在创建新订单时,应该增加章节的属性。 NSNumber * number = [NSNumber numberWithInt:order.orderNumber.intValue]; int value = [number intValue]; number = [NSNumber numberWithInt:value + 1]; order.orderNumber = [NSString stringWithFormat:@Order%@,number.stringValue]; NSLog(@%@,order.orderNumber); NSManagedObjectID * orderedProductID = [orderedProduct objectID]; [order addOrderProductsObject:(ProductOrder *)[context objectWithID:orderedProductID]]; 退货单 } : //购物车产品 [self.fetchedResultsController fetchedObjects]){ [[[DataManager sharedInstance] backgroundManagedObjectContext] performBlock:^ { [Order addOrderedProductToOrderWithOrderedProduct:[ProductOrder addProductToOrderWithProduct:prod inManagedObjectContext :[[DataManager sharedInstance] backgroundManagedObjectContext]] inManagedObjectContext:[[ prod.antal = [NSNumber numberWithInt:0]; [self saveCurrentContext:_theManagedObjectContext]; [[DataManager sharedInstance] saveBackgroundContext]; [[DataManager sharedInstance] saveMasterContext]; }]; } order viewcontrollerfrom passed order object NSFetchRequest * fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@ProductOrder]; NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@inOrder.orderNumberascending:YES]; [fetchRequest setSortDescriptors:@ [sortDescriptor]]; self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_theManagedObjectContext sectionNameKeyPath:@inOrder.orderNumbercacheName:nil]; _fetchedResultsController.delegate = self; NSError * error = nil; if(![_ fetchedResultsController performFetch:& error]){ NSLog(@Fetch Failed); } } 解决方案一个产品可以多次订购,一个订单包含多个产品,所以的第一个想法是使用多对多关系: 产品<< - >>但是,如果您要使用提取的结果显示其产品的所有订单$($ 1) $ b Product< - > ProductOrder< - >因为FRC可以只显示每个对象一次,所以使用模型()()() 1)您可以不显示作为多个订单的一部分的产品。使用模型(2),您可以通过 (使用)来获取的 ProductOrder sectionNameKeypath:参数)。 I currently have the following relationship set up for my ordering app.Product <<- CartThat's working as it should, the product gets added to the cart and so on. When the user has pushed the "order button" in the "cart viewcontroller", I want all these products, which just got ordered to show up, in my other view controller. I want to show all the orders made by the user. But I can't really figure out how to setup my Core data model in order to accomplish this? Do I need a one to one relationship from Cart or a one to many relationship or something completly different?Thank you for your help.EDIT 1:EDIT 2: + (ProductOrder *)addProductToOrderWithProduct:(Product *)product inManagedObjectContext:(NSManagedObjectContext *)context { ProductOrder *orderProduct = nil; orderProduct = [NSEntityDescription insertNewObjectForEntityForName:@"ProductOrder" inManagedObjectContext:context]; NSManagedObjectID *productID = [product objectID]; orderProduct.antal = product.antal; [(Product *)[context objectWithID:productID] addOrderedProductsObject:orderProduct]; return orderProduct;} + (Order *)addOrderedProductToOrderWithOrderedProduct:(ProductOrder *)orderedProduct inManagedObjectContext:(NSManagedObjectContext *)context { Order *order = nil; order = [NSEntityDescription insertNewObjectForEntityForName:@"Order" inManagedObjectContext:context];// THIS SHOULD INCREMNT THE ATTRIBUTE FOR SECTIONS, WHEN A NEW ORDER IS CREATED. NSNumber *number = [NSNumber numberWithInt:order.orderNumber.intValue]; int value = [number intValue]; number = [NSNumber numberWithInt:value + 1]; order.orderNumber = [NSString stringWithFormat:@"Order %@", number.stringValue]; NSLog(@"%@", order.orderNumber ); NSManagedObjectID *orderedProductID = [orderedProduct objectID]; [order addOrderProductsObject:(ProductOrder *)[context objectWithID:orderedProductID]]; return order; }Add products to order from products in cart:// Cart products for (Product *prod in [self.fetchedResultsController fetchedObjects]) { [[[DataManager sharedInstance] backgroundManagedObjectContext] performBlock:^{ [Order addOrderedProductToOrderWithOrderedProduct: [ProductOrder addProductToOrderWithProduct:prod inManagedObjectContext:[[DataManager sharedInstance] backgroundManagedObjectContext]] inManagedObjectContext:[[DataManager sharedInstance] backgroundManagedObjectContext]]; prod.antal = [NSNumber numberWithInt:0]; [self saveCurrentContext:_theManagedObjectContext]; [[DataManager sharedInstance] saveBackgroundContext]; [[DataManager sharedInstance] saveMasterContext]; }]; }Load the orders in "order viewcontroller" from passed order object NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"ProductOrder"]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"inOrder.orderNumber" ascending:YES]; [fetchRequest setSortDescriptors:@[sortDescriptor]]; self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_theManagedObjectContext sectionNameKeyPath:@"inOrder.orderNumber" cacheName:nil]; _fetchedResultsController.delegate = self; NSError *error = nil; if (![_fetchedResultsController performFetch:&error]) { NSLog(@"Fetch Failed"); }} 解决方案 A product can be ordered multiple times, and an order contains multiple products, so thefirst idea would be to use a many-to-many relationship:Product <<-->> Order (1)However, if you want to display all orders with their products using a fetched resultscontroller, then you need to insert an "intermediate" entity: Product <->> ProductOrder <<-> Order (2)The reason is that a FRC can display each object only once, so with model (1) you couldnot display products that are part of more than one order. With model (2), you can fetchthe ProductOrder objects and group them into sections by Order(using the sectionNameKeypath: parameter). 这篇关于在订购应用程序中设置Core数据关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-13 01:06