0

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 :)

Shai Mishali
  • 9,224
  • 4
  • 56
  • 83

1 Answers1

1
  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. :-)

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • My real app doesn't really use "Class", and that was definitely a bad idea for an example :) Unfortunately these items could be added at any time, so keeping all of the "Classes" , for that matter, in memory - won't make much sense I think :) So just fetch it, change it, and save the context ? :) Sounds easy enough, ill try it , thanks ! – Shai Mishali Apr 12 '12 at 15:12