So I have a self referencing, one-to-many relationship, Tags:
Tag
- Name: String
- Order: NSNumber
- SubTags: [Tag]
- ParentTag: Tag
A tag can have many subtags and can only have a single parent
My goal is to use NSFetchedResultsController to display them in this manner in a UITableView (where each tag is a cell, I'm using spaces to show hierarchy to parent):
Parent Tag, order 1
Child Tag, order 1
Child 2 Tag order 2
Parent 2 Tag, order 2
Child 3 Tag, order 1
Child 4 Tag, order 2
Is this even possible? Should I be storing and calculating the order keys differently so I can flatly order by that?
I've been racking my brain over this for a few days now and any advice is welcome :o)
Yes, you should. As it stands, to use an FRC, you need a single method by which to order all of the entity instances, but you can't use relationship order. You can do this by:
So you would have a hierarchy and order something like:
Parent 1 Tag, order 1
Child 1 Tag, order 1.1
Child 2 Tag, order 1.2
Parent 2 Tag, order 2
Child 3 Tag, order 2.1
Child 4 Tag, order 2.2
Child 5 Tag, order 2.2.1
You can tell the depth by counting the number of dots in the order, but it's better to store the depth number on the child, or at least store that depth as a transient and calculate it by counting the parents.