核心数据继承与关系 [英] Core Data Inheritance and Relationships

查看:48
本文介绍了核心数据继承与关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对核心数据中的继承和关系有些困惑,我希望有人可以驾车驶向正确的道路.在我的应用程序中,我创建了3个实体,但它们都没有(也不应该具有)公共属性,但是用户要做的所有工作都会有一个保存和加载按钮.根据我的理解,我需要将所有工作"实体包装"到一个用于保存和加载的对象中,我的问题是,是否需要在实体之间创建关系?因为我必须以某种方式联系他们,所以这对我来说很有意义.我的逻辑正确吗?

I´m a little confused about inheritance and relationships in core data, and I was hopping someone could drive to the right path. In my app i have created 3 entities, and none of them have (and are not suppose to have) common properties, but there´s gonna be a save and a load button for all the work that the user does. From my understanding I need to "wrap" all the entities "work" into an object which will be used to save and load, and my question is, do I need to create relationships between the entities? Because I have to relate them somehow and this is what make sense to me. Is my logic correct?

我正在安装一个预算计算器,为了让每个人都了解我的问题,我将举一个实际的例子,如果我的逻辑不正确,请纠正我:

I'm implementing a budget calculator, and for the purpose of everyone understand what my issue is, I´m going to give an practical example and please correct me if my logic is incorrect:

我们只是说您是一个水果销售商,因此拥有一个客户数据库和一个包含您所销售水果种类的水果数据库是正常的.据我了解,我在这里找到两个实体:

Let´s just say you are a fruit seller, and because of that it´s normal to have a database of clients and also a fruit database with the kinds of fruit you sell. From my understanding I find two entities here:

客户端,其属性名为:名称地址电话电子邮件

Client with properties named: name, address, phone, email, etc.

库存,其属性名为:名称重量库存 cost 供应商

Stock with properties named: name, weight, stock, cost, supplier, etc.

预算,其属性名为:名称金额类型 cost 投放

TheBudget with properties named: name, amount, type, cost, delivery, etc.

我没有列出所有属性,因为我认为您明白了.我的意思是,正如您所看到的,我只能继承两个属性.其余的是不同的.因此,如果我正在为客户制定预算,那么我可以拥有任意数量的客户以及所需的库存量,但是实际预算呢?

I didn´t put all the properties because I think you get the point. I mean as you can see, there´s only two properties I could inherit; the rest is different. So, if I was doing a budget for a client, I can have as many clients I want and also the amount of stock, but what about the actual budget?

很抱歉,如果我的解释不是很清楚,但是如果是..我应该建立什么样的关系?我认为 Client TheBudget 有联系.你对我有什么建议?

I´m sorry if my explanation was not very clear, but if it was..what kind of relationships should I be creating? I think Client and TheBudget have a connection. What do you advise me?

推荐答案

这不是完全正确的,但是某些部分在正确的轨道上.我将您的问题分为三个部分:关系,继承和托管对象上下文,希望可以帮助您分别理解每个部分:

That's not entirely correct, but some parts are on the right track. I've broken your question down into three parts: relationships, inheritance and the Managed Object Context to hopefully help you understand each part separately:

关系通常用于指示一个实体可以属于"另一个实体(即,雇员可以属于公司).您可以设置多个一对多关系(即,雇员属于公司和老板),并且可以设置逆向关系(用拥有"或具有"一词更好地描述,例如一个公司"有很多员工).

Relationships are usually used to indicate that one entity can 'belong' to another (i.e. an employee can belong to a company). You can setup multiple one-to-many relationships (i.e. an employee belongs to a company and a boss) and you can setup the inverse relationships (which is better described with the word 'owns' or 'has', such as 'one company has many employees).

取决于您的需求和整套删除规则,有很多甚至更复杂的关系,您可以告诉系统删除​​关系中的实体时要遵循的规则.刚开始时,我发现坚持上述一对一和一对多关系最容易.

There are many even more complicated relationships depending on your needs and a whole set of delete rules that you can tell the system to follow when an entity in a relationship is deleted. When first starting out I found it easiest to stick with one-to-one and one-to-many relationships like I've described above.

最好将继承描述为一种基本模板,用于其他更具体的实体.您说对了,您可以使用继承作为一种协议来定义一些在多个实体之间通用的基本属性,这是正确的.一个很好的例子是拥有一个具有属性名称",地址"和开始日期"的雇员"基类.然后,您可以创建从该Employee实体继承的其他实体,例如"Marketing Rep","HR","Sales Rep"等,它们都具有共同的属性名称",地址"和开始日期",而无需在每个单独的实体上创建这些属性.然后,如果您想更新模型并添加,删除或修改公共属性,则可以在父实体上执行此操作,并且其所有子实体都将自动继承这些更改.

Inheritance is best described as a sort of base template that is used for other, more specific entities. You are correct in stating that you could use inheritance as a sort of protocol to define some basic attributes that are common across a number of entities. A good example of this would be having a base class 'Employee' with attributes 'name', 'address' and 'start date'. You could then create other entities that inherit from this Employee entity, such as 'Marketing Rep', 'HR', 'Sales Rep', etc. which all have the common attributes 'name', 'address' and 'start date' without creating those attributes on each individual entity. Then, if you wanted to update your model and add, delete or modify a common attribute, you could do so on the parent entity and all of its children will inherit those changes automatically.

现在,在问题/陈述的另一部分:将所有实体包装到一个对象中,该对象将用于保存和加载.您无需创建此对象,核心数据专用于此目的使用NSManagedObjectContext(简称MOC).MOC的任务是跟踪您创建,删除和修改的对象.为了保存更改,您只需在MOC上调用 save:方法.

Now, onto the other part of your question/statement: wrapping all of your entities into an object which will be used to save and load. You do not need to create this object, core data uses the NSManagedObjectContext (MOC for short) specifically for this purpose. The MOC is tasked with keeping track of objects you create, delete and modify. In order to save your changes, you simply call the save: method on your MOC.

如果您发布实体及其功能,我也许可以帮助您提出在核心数据中设置实体的方法的建议.您希望尽最大的努力在初始开发过程中建立尽可能强大的核心数据模型.操作系统需要能够升级"后备存储,以合并您在核心数据模型修订版之间进行的任何更改.如果您最初设置核心数据模型并以这种方式发布代码的工作做得很差,那么在应用程序泛滥的情况下尝试进行复杂的模型更新可能会非常困难(您可能已经猜到了,是来自痛苦经历的建议:)

If you post your entities and what they do, I might be able to help make suggestions on ways to set it up in core data. You want to do your best to setup as robust a core data model as you can during the initial development process. The OS needs to be able to 'upgrade' the backing store to incorporate any changes you've made between your core data model revisions. If you do a poor job of setting up your core data model initially and release your code that way, it can be very difficult to try and make a complicated model update when the app is in the wild (as you've probably guessed, this is advice born out of painful experience :)

这篇关于核心数据继承与关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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