核心数据:如何使用父实体构建数据模型
我是一个长期的听众,但第一次来电…希望你能帮助我解决一个核心数据问题!
我正在为 iPad 创建一个自定义业务应用程序,允许用户填写其业务中常用的合同。有四个合同,每个合同有 20 到 40 个数据字段,它们都非常不同,但是有一些 (3-4) 个字段是所有四个合同所共有的,例如创建日期和供应商名称等。
我需要显示所有合约都放在一个 UITableView 中(分组样式以显示不同类型),我需要能够选择一个单元格并显示详细视图,以便用户可以查看和修改特定合约的数据。
迄今为止我的研究告诉我,我应该使用一个父实体(实体 A)列出所有共有的属性,然后使用具有这些属性的子实体(实体 B、C、D 和 E 代表四个合约中的每一个)它们是唯一的,并从实体 A 继承其余部分。
这是构建此数据模型的最佳方式吗?请记住,当我创建合同的新实例时,它需要是一个独立的对象,包含其所有属性以及父对象的属性。如果是这样,当我为 tableView 生成单元格时,我是否获取实体 A 并希望其余实体也随之出现,或者我是否为每个合同类型单独获取请求并以这种方式添加到表中?相反,如果我想创建一个新的合约实例(比如我的数据模型中的实体 B),我该如何做?
我希望我已经解释得足够好了。感谢您对此的帮助!
I’m a long time listener, but first time caller here… Hoping you can help me with a core data question!
I am creating a custom business app for iPad that allows a user to fill out contracts commonly used in their business. There are four contracts with between 20 and 40 data fields each and they are all quite different, but there are a few (3-4) fields that are common to all four, such as Date Created and Vendor Name etc.
I need to display all the contracts together in a UITableView (grouped style to show there are different types), and I need to be able to select a cell and display a detail view so the user can view and modify data on a specific contract.
My research to date tells me I should use a parent entity (entity A) that lists the attributes common to all, and then child entities (entity B, C, D, & E representing each of the four contracts) that have the attributes unique to them, and inherit the rest from entity A.
Is this the best way to structure this data model? Bearing in mind that when I create a new instance of a contract it needs to be a standalone object with all its attributes plus that of the parent. If so, when I’m generating cells for my tableView, do I fetch entity A and hope the rest come along also, or do I do separate fetch requests for each contract type and add to the table that way? Conversely, if I want to create a new instance of a contract (say entity B in my data model) how would I go about that?
I hope I’ve explained that well enough. Thanks for your help with this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用公共字段创建一个抽象实体,并从那里继承不同的合约类型。来自文档:
这还将使您能够为所有合约定义通用行为,并在代码中使用抽象超类来引用任何合约。
You should create an abstract entity with your common fields, and inherit your different contract types from there. From the documentation:
This will also enable you to define common behaviours for all your contracts and use the abstract superclass in your code to refer to any contract.