Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

swift - Core Data: Transformable Type or To-Many Relationship?

I'm a rookie and having a hard time wrapping my head around core data.

I have created an app that works fine saving to the file manager, but gets slow with lots of data in it. I would like to upgrade this to core data.

I've successfully created some entities out of my basic classes that persist just fine, but when I have a class that has properties of other classes as arrays, I'm not sure what direction to go to have it save in core data.

Simple Example:

class Student {
  let name: String
  let markbook: [Markbook]
}

class Markbook {
  let name: String
  let components: [MarkbookComponent]
}

Each student instance should have their own markbook and each markbook will have multiple markbook components that can have individual student marks updated.

For my core data Student entity, what would be the right approach: do I make markbook transformable type to achieve an array of markbooks, or do I make a Markbook entity with a to-many relationship to Student? Does that to-many relationship give each student their own individual markbook?

question from:https://stackoverflow.com/questions/66056212/core-data-transformable-type-or-to-many-relationship

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use relationships; your problem is what they were invented for.

(Although it's possible to use transformable attributes to represent arrays of objects:

  1. it's not possible to have arrays of other NSManagedObjects (CoreData gets confused);
  2. 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
  3. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...