添加到购物车功能 - 核心数据 [英] Add to cart functionality - Core data

查看:146
本文介绍了添加到购物车功能 - 核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为以下场景创建最佳的设计。



类别 - >产品列表



在显示产品列表的表格中,用户可以选择将该产品添加到购物车。

因此,每当用户打开购物车时,他都可以看到所有产品。



我的问题是我如何在Core Data中最好设置此功能?是维护单独的购物车实体的最佳解决方案吗?或者是一种关系是正确的方式吗?如果是,你能告诉我什么样的关系,它应该在Cart&产品?

解决方案

按照您建议的创建购物车实体与 CartProduct 实体之间的多个 关系。 购物车实体可以包含有关购买的meta信息, CartProduct 会链接到实际的实体



CartProduct 实体与 Product 实体本质上是一个1-1,因此在Xcode中,在数据模型编辑器中,选择 CartProduct 请务必在 关系部分中添加产品实体作为关系,并保留 < - 多个关系 设置。值如下:




  • 关系:产品

  • 目的地:产品

  • Inverse:CartProduct



然后,选择产品实体,并仔细检查是否存在类似于此的关系(再次确保 To-Many Relationship 设置未选中):




  • 关系:CartProduct

  • 目的地:CartProduct

  • 产品



以上内容应该让您入门。


I am trying to create the best possible design for the following scenario.

Category -> The list of products

In the tableview where the list of products is shown, the user has the option to add that product to the cart.
So, whenever the user opens the cart, he can see all of the products he has added.

My question is how can I best setup this functionality in Core Data? Is the best solution to maintain a separate Cart Entity? Or is a relationship the right way to do it?, If yes can you please tell me what kind a relationship it should be between Cart & Product?

解决方案

Make a Cart entity as you suggested with a To-Many relationship with a CartProduct entity. Cart entity can contain 'meta' information about the purchase and CartProduct would link to the actual Product entity.

The type of relationship between a CartProduct entity and Product entity would essentially be a 1-1, so in Xcode, in the data model editor, with the CartProduct entity selected, be sure to add the Product entity as a relationship in the Relationships section and leave the To-Many Relationship setting unchecked. The values would be something like this:

  • Relationship: Product
  • Destination: Product
  • Inverse: CartProduct

Then, select the Product entity in the editor and double check that a relationship exists that looks something like this (again, make sure that the To-Many Relationship setting is unchecked):

  • Relationship: CartProduct
  • Destination: CartProduct
  • Inverse: Product

The above should get you started.

这篇关于添加到购物车功能 - 核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆