Showing posts with label to many. Show all posts
Showing posts with label to many. Show all posts

Monday, August 14, 2017

Swift 3 - CoreData - To Many - Inverse Relationship (Tutorial-5)

In the last tutorial we have seen 'To One - Inverse relationship' with a person spouse as an example.

In this tutorial, Let’ see To Many - Inverse relationship.

Please checkout code for this example here

In this example, each person can have a manager (To One) and n number of reportees (To Many) If the person is a manager.





Here, 

Relationship ‘reportees’ 


If Person A is added as a reportee to Person B, as we have given Inverse relationship as manager,
Person B will become the manager for that reportee automatically.

Person A.manager = Person B (Inverse Relationship as manager)

As a person can have multiple reportees If he/she is a manager, reportees is having To Many type relationship with the person.

Relationship ‘manager’ 


If Person A is added as a manager to Person B, as we have given Inverse relationship as reportees,
Person B will become the reportee for that manager automatically.

Person B is now one of the reportees to Person B

As a person can have only one manager, manager is having To One type relationship with the person.


When there is a To Many relationship, Coredata gives it as a NSSet, which is unordered list with no duplicates.

Download the example and see the Person core data class.


  
   @NSManaged public var manager: Person?
    @NSManaged public var reportees: NSSet?



The beauty of core data is, It gives all accessor methods for adding and removing reportees when we create a Person NSManagedObject subclass.



  
extension Person {
    
    @objc(addReporteesObject:)
    @NSManaged public func addToReportees(_ value: Person)
    
    @objc(removeReporteesObject:)
    @NSManaged public func removeFromReportees(_ value: Person)
    
    @objc(addReportees:)
    @NSManaged public func addToReportees(_ values: NSSet)
    
    @objc(removeReportees:)
    @NSManaged public func removeFromReportees(_ values: NSSet)
    
 }



In the example, If the person is a manger, They can select reportees, Where I am using generated accessor method “addToReportees()”

Add Reportee


Reportee Selection


Added Reportee


Manager updated (Inverse Relationship)


Hope this post is useful. Feel free to comment incase of any queries.


Friday, July 1, 2016

Objective C NSPredicate Part 4/4

In my earlier posts on NSPredicate, We have discussed,


In this part, let's see some Relational operations along with compound predicates.

  • IN    
  • ALL
  • ANY
  • SOME
  • NONE


We have already covered IN operation in Part 3/4. To discuss remaining Relational operations, let's take another data set.

Let's say fetching all the departments in an organization given the below JSON response.

[

  {

    "name" : "Banking",

    "employees" : [

      {
        "name" : "Tim",
        "salary" : 45000
      },

      {
        "name" : "Smith",
        "salary" : 19000
      },

      {
        "name" : "George",
        "salary" : 30000
      }

    ]

  },


  {

    "name" : "Healthcare",

    "employees" : [

      {
        "name" : "Martin",
        "salary" : 45000
      },

      {
        "name" : "Smith",
        "salary" : 56000
      },

      {
        "name" : "Anderson",
        "salary" : 24000
      }

    ]

  },


  {

    "name" : "Mobility",

    "employees" : [

      {
        "name" : "Martin",
        "salary" : 45000
      },

      {
        "name" : "Smith",
        "salary" : 17000
      },

      {
        "name" : "Anderson",
        "salary" : 24000
      }

    ]

  }


]


This response is in array format and it is having all the departments with their name and employees set.

Let's start our filtration without which we can't live :)





#1 Filter the departments having employee salary less than 20000

Technically, We need all the departments having any of the employee's salary less than 20000.

    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                                                         @"ANY employees.salary < %d",20000];
    
    NSArray *filteredArr = [arrDepartments filteredArrayUsingPredicate:predicate];
    
    NSLog(@"filteredArr : %@",filteredArr);

  filteredArr : (
        {
        employees =         (
                        {
                name = Tim;
                salary = 45000;
            },
                        {
                name = Smith;
                salary = 19000;
            },
                        {
                name = George;
                salary = 30000;
            }
        );
        name = Banking;
    },
        {
        employees =         (
                        {
                name = Martin;
                salary = 32000;
            },
                        {
                name = Smith;
                salary = 17000;
            },
                        {
                name = Anderson;
                salary = 24000;
            }
        );
        name = Mobility;
    }

  )




#2 Filter the departments having all the employees salary greater than 20000



    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                                                          @"ALL employees.salary > %d",20000];
    
    NSArray *filteredArr = [arrDepartments filteredArrayUsingPredicate:predicate];
    
    NSLog(@"filteredArr : %@",filteredArr);


  filteredArr : (
        {
        employees =         (
                        {
                name = Martin;
                salary = 45000;
            },
                        {
                name = Smith;
                salary = 56000;
            },
                        {
                name = Anderson;
                salary = 24000;
            }
        );
        name = Healthcare;
    }

  )

    



#3 Filter the departments having none of the employees salary less then 20000



    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                                                       @"NONE employees.salary < %d",20000];
    
    NSArray *filteredArr = [arrDepartments filteredArrayUsingPredicate:predicate];
    
    NSLog(@"filteredArr : %@",filteredArr);

  filteredArr : (
        {
        employees =         (
                        {
                name = Martin;
                salary = 45000;
            },
                        {
                name = Smith;
                salary = 56000;
            },
                        {
                name = Anderson;
                salary = 24000;
            }
        );
        name = Healthcare;
    }

  )

   


Hope this post is useful. Feel free to comment in case of any queries.