-1

So, I have a core data database with multiple relationship already populated with data. My problem is that I created the relationships between the tables but i don't know how to set them in code. I only need to set one and then I can figure it out for the rest of them. 2 core data tables For example: Colleges has the "has Groups" relationship because one college has many groups. The Groupe table has the "bellongsToColegiu" relationship becouse one or more groups are in one college.

I know that, i have to set the ".bellongsToCollegiu = Colegiu?" or ".hasGrupe = Grupe?" but i don't know how to fetch the college in proper way.

enter image description here

For now I have only one college in database.

Can someone help me with this please? I've been knocking my head for a few days and got nothing.

Thank you and have a nice day!

Mastertrap
  • 59
  • 1
  • 11
  • Are you trying to create a new relationship between entities *or* assigning entities to an existing relationship? – bbarnhart Apr 11 '18 at 13:06
  • I'm trying to create new relationship between entities. I have all the data in tables but i can't use it properly because I didn't set the relationships between them. – Mastertrap Apr 11 '18 at 13:10
  • [How to create relationship programmatically](https://stackoverflow.com/questions/13743242/adding-relationships-in-nsmanagedobjectmodel-to-programmatically-created-nsentit) – bbarnhart Apr 11 '18 at 13:12
  • Once you have the relationship created, you still need to assign entities to be part of the relationship. – bbarnhart Apr 11 '18 at 13:13
  • It looks like you've set the relationships already in your model but are needing to assign entities to a relationship. – bbarnhart Apr 11 '18 at 13:15
  • Sorry, yes. I've created the relationships and now I have to assign entities to them. Ex: I have to add that the "Grupe" entity belongs to "College" entity. but i don't know how to do this. – Mastertrap Apr 11 '18 at 13:23

2 Answers2

-1

You can fetch a existing 'Colegiu' from the data base or create a new one. for both fetching and creating new one you would need a managed object context. Managed object context is the last part of the data stack which has 3 components comprising of 1. Persistence store, 2. persistence store coordinator, and 3. managed object context.

Here is how to create a stack: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/InitializingtheCoreDataStack.html

Then you can create a new object:

let newItem = NSEntityDescription.insertNewObject(forEntityName: 'Colegiu',
                                                          into: 'reference to managed object context here')

or you can search the existing one by calling fetch on managed object context,

Keep in mind CoreData is a complicated topic and it takes time to put the head around. All the best.

Congruent Tech. UG
  • 1,408
  • 2
  • 12
  • 21
  • I have the context created. With context i saved the data. `let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext .` – Mastertrap Apr 11 '18 at 12:56
  • The fetch request gives me an array like this "[Colegiu]" the .belongsToColegiu property need to be of tipe "Colegiu?" and i don;t know how to do this.... – Mastertrap Apr 11 '18 at 13:04
-1

When you create entities and relationship between them Xcode also generates code for handling those relationships. The name pattern for code that handles relationships are

  1. The same as the name of the relation for to-one relationships
  2. addToX and removeFromX (x is the relationship) for to-many relationship.

So you should have for instance addToHasGrupe(_ value: Grupe) on College and that you can add a group instance to and Grupe should have a belongsToColegiu property that you can set to a college instance. Autocompletion should help you.

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • Thank you so much. Finally I figure it out. It's ok if I put the relationship just in one way? Or i have to put it from both entities? Have a nice day ! For example: If i put to Groups entity that belongsToColegiu, it's enough ? Or i have to put the inverse relationship (addToHasGrupe)? – Mastertrap Apr 12 '18 at 06:25
  • Yes, it's enough to do it one way since you have an inverse relationship defined Core Data handles the rest for you. – Joakim Danielson Apr 12 '18 at 07:24