Brandon Schlenker
Brandon Schlenker

Reputation: 5088

CoreData Arrays

Currently I need to set up a database that uses the following structure.

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?

Upvotes: 4

Views: 889

Answers (2)

akashivskyy
akashivskyy

Reputation: 45210

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! :)

Upvotes: 4

PeyloW
PeyloW

Reputation: 36762

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.

Upvotes: 4

Related Questions