In the application I am working on, I have a note object. This note object can have one super note and many sub-notes; essentially, I am creating a note hierarchy. The way in which it functions is actually very similar to UIView.
Anyhow, I need to be able to save this data. I've worked with Core Data and understand that you can create relationships between particular objects and each of these managed objects can have primitive properties (excepting transformable types). However, I am unsure whether creating a relationship between a note, its superview, and its subviews is the most practical-or even possible- way to do this.
That being said, I wanted to get some perspective. If I have to save an array of notes, and many of these notes have references to each other (in the same way that views do), what is the best way to do this?
Here's how I would do it:
1) Use Core Data
and (optionally) Mogenerator. Especially as your app's functionality continues to grow, you'll be glad you did.
2) Create a Note
entity in the data model.
3) On the Note
entity, create a to-many relationship, called notes
, that points to Note
(to itself) with an inverse to-one relationship, called parentNote
. So, you would have a two way relationship like this:
parentNote
<<---> notes
(meaning: one parentNote
and one or more notes
)
Point of interest
As you mentioned, UIView
does something quite similar to this. Basically, each UIView
has an array of subviews
and each subview
references its parent view, superview
. So you have this two-way relationship on each UIView
:
superview
<<---> subviews
Screenshots per request:
Note
entity showing the relationships only:
The notes
relationship:
The parentNote
relationship:
Also of interest are the delete rules
- I'd recommend you set cascade
on notes
and nullify
on parentNote
relationship (this would imply that a parentNote
owns its children notes
, so when it is deleted, they are too). However, make sure this is the behavior you want.