Swift iOS:将数据插入CoreData中的相关实体 [英] Swift iOS : Insert data into related entities in CoreData
问题描述
我试图理解CoreData中一对多关系实体中保存和检索记录的概念。我有两个表产品(主)和销售(详细)因此(一对多)关系关系是(销售)。
问题1:向产品注册销售。我创建一个Sales实体的实例,填充值,然后将其添加到product.sales(关系)并尝试managedObjectContext保存。
第一个问题:我还必须在销售实体中输入销售实体的实例吗?
第二个问题:如果我想查询到目前为止的所有销售,我查询销售实体吗?或者查询产品实体中的关系?
感谢您的帮助!
您的销售关系看起来像这样: / p>
关系只是一个依赖关系两个实体,因此在两个实体之间总是存在一个反比关系,确保如上所示将它们连接起来。
为什么我告诉你这个?记住我提到它取决于你做什么实体你的查询?这是重要的。
例如,如果您希望给予产品$ c>的
Sale
$ c>,您将查询产品本身(通过查询其关系 sale
):
let product = [您的Core Data存储的产品实例]
let sale = product.sale //返回销售产品与
相关联
如果您想查看特定销售的所有产品,您可以查询销售
products
关系:
let sale = store]
let products = sale.products //包含在销售中的产品
您希望指定日期的所有销售:
这不会对查询 / code>实体,因为每个产品只与它包含的销售有关系。
因此,要回答你的问题, 销售
实体检索给定日期的所有销售。
我希望有帮助,让我知道是否有不清楚的地方。
I am trying to understand the concept in saving and retrieving records in a one-to-many relationship entities in CoreData. i have two tables Products (Master) and Sales (Details) hence (one-to-many) relationship with the relationship key being (sales).
Issue 1: when registering a sale to a product. i create an instance of the Sales entity, populate the values, then add it to the product.sales (relationship) and attempt a managedObjectContext save.
First Question: Do i also have to enter the instance of the Sales entity into the Sales Entity? or that will be updated automatically since there is a relationship?
Second Question: If i want to query all the sales that happened to date, do i query the Sales Entity? or do i query the relationship in the Products Entity?
Thanks for your help!
First Question: Do i also have to enter the instance of the Sales entity into the Sales Entity? or that will be updated automatically since there is a relationship?
Something you might not realize here is that you are inherently saving the object by adding it to the managedObjectContext
. As soon as you do something like
let sale = Sale(context: managedObjectContext)
followed by
managedObjectContext.save()
the context issues a save request to your persistent store (your actual SQL database).
Therefore your question whether you need to store the Sale as well is answered, it will always be stored upon saving the context.
Second Question: If i want to query all the sales that happened to date, do i query the Sales Entity? or do i query the relationship in the Products Entity?
That depends...
First let me give you a little tip/best practise:
Always make sure to set up an inverse relationship
In the Core Data Editor for your Product
entity's relationships you can do something like this:
Your sales relationships look something like this:
A relationship is nothing more but a dependency between two entities, therefore there is always an inverse relationship between two entities, make sure you hook them up as shown above.
Why am I telling you this ? Remember I mentioned it depends what entity you do your query on ? This is where it matters.
For example, if you want the Sale
for a given Product
, you would query the product itself (by querying its relationship called sale
):
let product = [A product instance from your Core Data store]
let sale = product.sale // returns the sale the product is associated to
If you want all the products from a given sale, you would query the Sale
entity leveraging the products
relationship:
let sale = [A sale from your Core Data store]
let products = sale.products // the products contained in the sale
You mentioned that you want all the sales to a given date:
It would not make any sense querying the Product
entity for that because each product only has a relationship to the sale it is contained in.
So, to answer your question, you should query the Sale
entity to retrieve all the sales to a given date.
I hope that helps, let me know if something is unclear.
这篇关于Swift iOS:将数据插入CoreData中的相关实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!