Search code examples
objective-csqlitecore-dataentity-relationship

Understanding CoreData using relationship


I'm having a misunderstanding using CoreData and relationship.

What i remember from my sql's time ( long time ago... ) i have to add and ID to each tables which will be the entry which make relation between tables.

Below the overview of what my tables looks like in my CoreData (entities).

Epro_Sheet is my main table, the others have to be related to that one.

What i want to do is that :

  • The user fills fields on a UIViewController (EPro_CompanyViewController) which has his own Entity EPro_Company
  • Then when he press next - that create EPro_Sheet, increment idSheet and fill the others entries - moreover the EPro_Company are filled with the fields text of the EPro_CompanyViewController.
  • Then the user will fill up fields of EPro_DaFroidViewController, then when he press next it fill the entity EPro_DaFroid get the current Id of the current EPro_Sheet and set it to EPro_DaFroid...
  • Etc....
  • There will be a UIViewController ( History ) which allows the user to retrieve all information of a specific date to either edit it or display it.

Could you help me on the way how to do it ? relationship or not at all ... if there is relationship how can i retrieve information using a NSPredicateFormat like : retrieve me all information from the date @" .... ";

thanks a lot for any help !

enter image description here


Solution

  • First of all you have to understand that core-data is not a relational database and the concept of primary key etc is not there. It's left to you for implementation. More details on this can be found here :

    Core-data vs RDBMS

    and here

    The differences between Core Data and a Database

    If you have want to implement relationship in core-data you have to create a relationship in the core-data model. Imagine you have relationship like this:

    Student <->> Section

    i.e one student belongs to one section and one section can have many students. Then in your core-data model you can define a relationship (say oneSection) from Student to Section that is one-to-one and another relationship(say manyStudents) from Section to Student that is one-to-many. And then you set the relationship as the inverse relationship of each other.

    Although we declare this as relationship , it pretty much acts as a property of the managed object.

    So if you have an managed objects called studObj1 and studObj2 (Student Entity), then you set the relationship like this to a section object (sectionObj):

    (assuming you will be creating a managed object subclass)

    Student *studObj1= <create object>
    Student *studObj2= <create object>
    
    Section *sectionObj = <create object>
    
    //set one-to-one relationship
    studObj1.oneSection = sectionObj;
    studObj2.oneSection = sectionObj;
    
    //set one-to-many relationship;
    
    NSSet *studSet = [NSSet setWithObjects:studObj1,studObj2,nil];
    sectionObj.manyStudents = studSet;
    

    Regarding predicate you can use the properties of the relationship directly: ie. if you want to get students based on section you can create a predicate like the following:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"oneSection.name like 'MainSection'"];