简体   繁体   中英

Understanding adding/updating relational objects in Core Data

I have , for example, two entities called Class and Student . Both entities are connected by a One-To-Many relationship, "Class" has a ClassID field and Student has both a ClassID and StudentID fields.

I have a couple of questions on this situation:

  1. When I define a Connection between Class and Student, are they connected by ClassID , or Core Data creates its own connection somehow?
  2. After having a Class entity already saved, lets say I want to add a Student to it. Could I just add a new Student with that ClassID, Or I would have to get the Class object, add a student to its .students NSSet object, and save it back to Core Data ?
  3. If the latter option is the only option, how would I go on updating a record in Core Data? Fetch is with a NSFetchRequest, change it and than just save the Managed Object Context again ?

Sorry for the many question and I greatly appreciate any assistance on this :)

  1. Your ClassID is just user data as far as Core Data is concerned. It doesn't take part in the relationships.

  2. Second option. Core Data persists objects and their relationships, so the strategy is to update the objects and then save them.

  3. Yes, though it's frequently true that apps will already have a related record loaded for the purpose of having the user indicate the connection they want.

Calling your Class class 'Class' is going to be confusing. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM