Use relationships; your problem is what they were invented for.
(Although it's possible to use transformable attributes to represent arrays of objects:
- it's not possible to have arrays of other
NSManagedObjects
(CoreData gets confused);
- you have to maintain or compute inverse relationships yourself (in your example, if you have a
Markbook
and wish to know which Student
it relates to, you'd have to scan through all the Student
objects to locate it. If you create an inverse relationship in CoreData, it will be populated and managed for free by CoreData); and
- transformable attributes cannot be "inspected" as part of a predicate for a fetch request. Hence if you wish to fetch all
Markbooks
with a MarkbookComponent
titled "English", you'll have to fetch ALL Markbooks
first, and then filter them in memory.)
As regards the cardinality of the relationships, if each Student
can have at most one Markbook
, then the relationship can be "to-one" (and will be defined in the class as a property of type Markbook
rather than an array [Markbook]
). Assuming each Markbook
likewise relates to only one Student
, the inverse relationship will also be "to-one".
As each Markbook
can have more than one MarkbookComponent
, model the relationship as "to-many" (which will then be defined in the class as a set of Markbooks
). I'm not sure from your description whether a MarkbookComponent
can relate to only one Markbook
, or more than one: model the inverse relationship as "to-one" or "to-many" as appropriate.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…