0

I'm basically trying to recreate a folder system using Core Data. I have an entity (folder) that that holds many of the same entities (folders). While initialising Core Data Stack I can display all of the folders, but I'm not sure how to approach storing an always persistent Core Data entity that will be a "root" folder for all others. (Think of it as 'Macintosh HD' in finder).

I was thinking of creating a separate root entity that has no deletion rule and count it as "root" folder and then add folders to it (while creating a new folder I could just use functions provided by Core Data relationships and add to the root entity). Could this be a proper approach?

devon't
  • 81
  • 4
  • you create a root entity and add folder reference to it. create folders in document directory with some unique name and store name in to entity or some other place. – Zeeshan Ahmad II May 24 '22 at 10:37
  • 1
    You are talking about a tree structure. The entity must have a to-many relationship `children` and a to-one relationship `parent`. The `parent` relationship of the root object is `nil`. – vadian May 24 '22 at 10:42
  • https://stackoverflow.com/questions/58845896/swiftui-fetchrequest-core-data-changes-to-relationships-dont-refresh/70959918#70959918 – lorem ipsum May 24 '22 at 11:14
  • In Core Data, the model design depends on how you want to query and sort the data to show in your UI. E.g. Notes has an interesting design that it has a special All iCloud folder that shows all the notes across all the folders. To show this special folder in the same list as actual folders they use entity inheritance so I would suggest looking a that. And that special default folder is a saved record. – malhal May 24 '22 at 14:36
  • If all folders must have a parent, either another folder or root, then you could create a specific type for root but not store it in core data to avoid having an entity with only one object. You could then create the root object at start up for instance and fetch all folders with parent is nil and assign them as children to your root object. – Joakim Danielson May 24 '22 at 15:55

0 Answers0