CoreData Arrays
Currently I need to set up a database that uses the f开发者_JAVA百科ollowing 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?
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! :)
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 a NSSet
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.
精彩评论