views:

32

answers:

1

Hi, I have used CoreData a couple of times but data modeling was very simple. This time I am supposed to implement the following:

  • User can create documents and folders (they don't have to be real folders/directories).
  • Folders can contain documents or other folders
  • Documents have:
    • 1 Title,
    • 1 Description and
    • 8 children
  • Each child also has a Title and a Description and could have 8 children (optionally)
  • This could be as many levels (recursive) as the user desires.

I am not sure how to express this in CoreData. Could someone give a hand here? Right now I think of:

"Child" entity with "Title" "Description" as attributes and "Children" as a relationship (one to many). "File" entity with "IsFolder" as a boolean attribute and "Documents" as a relationship (one to many, pointing to "Child")

I am not sure if this well implements above structure in CoreData. I am in the right path? Re-modeling data structure in CoreData could be painful (I've heard) so i would like to have a good structure from the beginning. I hope I can get some advice from you;)

Thanks in advance.

+2  A: 

You probably want something like this (pseudocode):

Folder{
    parent<<--(required,nullify)-->Folder.folders
    folders<--(optional,cascade)-->>Folder.parent
    documents<--(optional,cascade)-->>Document.folder
}

Document{
    title:string
    descriptionText:string
    parent<<--(optional,nullify)-->Document.children
    children<--(optional,cascade)-->>Document.parent
    folder<<--(optional,nullify)-->Folder.document
}

(Word of warning: Don't ever use "description" as an attribute name. NSObject has a description method so every subclass of NSObject responds to the description message. It will cause all kinds of ugly problems if you have an attribute accessor of the same name.)

TechZen