核心数据数组
目前我需要建立一个使用以下结构的数据库。
- 项目1
- 对象A
- 东西A1
- 东西A2
- 东西A3
- 对象B
- B1 内容
- 对象A
我的第一个想法是创建 3 个实体,一个用于“Item”、“Object”和“Stuff”,然后每个实体都有一个数组。 因此,每个“项目”都有一个包含“对象”的数组,每个对象都有一个“东西”数组。
这是一个好方法吗?如果不是,那么完成相同任务的更正确和更有效的方法是什么?
Currently I need to set up a database that uses the following structure.
- Item 1
- Object A
- Stuff A1
- Stuff A2
- Stuff A3
- Object B
- Stuff B1
- Object A
My first thought was to create 3 entities, one for "Item", "Object", and "Stuff", then have an array in each.
So each "Item" would have an array containing the "objects", and each object with an array of "stuff."
Would this be a good approach? If not, what would be a more correct and efficient way of accomplishing the same task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这些情况下,当您有 2 个实体并且希望在它们之间建立关系时,您应该尝试CoreData 的关系。在这种情况下,您应该使用对多关系。
PS:不要忘记选择您的关系删除规则! :)
In theese situations, when you have 2 entities and you want to have a relation between them, you should try out CoreData's relationships. In this case you should use to-many relationship.
PS: Don't forget to select your relationship delete rule! :)
您创建 3 个实体的方法是正确的方法。这些实体之间的连接就是 Core Data 所说的“关系”。您需要注意的是,Core Data 仅处理无序关系。因此,对象 A 不会获得 Stuff 的
NSArray
,它将拥有 Stuff< 的NSSet
/em>。如果需要排序,那么您需要使用子实体的属性进行排序,并使用
NSFetchRequest
获取这些对象。例如,对某些“名称”或“日期”属性进行排序。Your approach to create 3 entities is the right way. The connections between these entities is what Core Data refer to as relations. You need to take note that Core Data only handles unordered relations. So Object A will not get an
NSArray
of Stuff, it will have aNSSet
of Stuff.If ordering is required then you need to use an attribute of the sub-entity for sorting, and fetch these objects using an
NSFetchRequest
. For example sorting on some "name" or "date" attribute.