Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreData Arrays

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

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?

like image 610
Brandon Schlenker Avatar asked Feb 25 '23 06:02

Brandon Schlenker


2 Answers

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

like image 60
akashivskyy Avatar answered Mar 03 '23 15:03

akashivskyy


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.

like image 35
PeyloW Avatar answered Mar 03 '23 16:03

PeyloW