title: Populating and Fetching Core Data One to Many Networked JSON Objects using Generic Method
body: <p>I have two entities, entity 1:Categories and entity 2"Companies which have a one-to-many relationship.</p>
  
  <p>The data for each is downloaded using a NSURLSession. The JSON objects in the categories entity have an array of all the ids of the companies and the company JSON object has the id of its category.</p>
  
  <p>I have created a generic method where the JSON attribute names match that of the entity attributes, so they will be added automatically.</p>
  
  <p>I know since there is an inverse relationship, any data added in one entity will be reflected in both entity objects. But my question is which should I add first? I mean if I add the categories, then I will have to create objects of companies which have only ids and then I will have to create an NSpredicate when adding any company and check whether the id exists and add the remaining data. </p>
  
  <p>The problem is I don't know how to incorporate this in my generic method and I'm wondering is there another way to do this? I don't want to use MagicalRecord as I'm trying to understand the concepts.</p>
  
  <pre><code>- (void) addDataToEntity:(NSArray *)jasonArrayData : (NSNumber *) entitySpecifier{
  
      //Flag to determine entity name
      NSString *entityName;
  
      if([entitySpecifier isEqual:@2]){
          entityName = @"Categories";
  
      }else if ([entitySpecifier isEqual:@3]){
          entityName = @"Companies";
      }
  
      //get entity
      NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:self.elRehabCoreData.managedObjectContext];
  
      //get attributes of entity in core data
      NSDictionary *attributes = [entity attributesByName];
  
  
      for (NSDictionary *keyedValues in jasonArrayData) {
  
          //loop over entities in core data
          for (NSString *attribute in attributes) {
              id value = [keyedValues objectForKey:attribute];
  
              if (value == nil) {
                  // Don't attempt to set nil, or you'll overwite values in self that aren't present in keyedValues
                  continue;
              }
              NSAttributeType attributeType = [[attributes objectForKey:attribute] attributeType];
  
              if ((attributeType == NSStringAttributeType) &amp;&amp; ([value isKindOfClass:[NSNumber class]])) {
                  value = [value stringValue];
              } else if (((attributeType == NSInteger16AttributeType) || (attributeType == NSInteger32AttributeType) || (attributeType == NSInteger64AttributeType) || (attributeType == NSBooleanAttributeType)) &amp;&amp; ([value isKindOfClass:[NSString class]])) {
                  value = [NSNumber numberWithInteger:[value integerValue]];
              } else if ((attributeType == NSFloatAttributeType) &amp;&amp;  ([value isKindOfClass:[NSString class]])) {
                  value = [NSNumber numberWithDouble:[value doubleValue]];
              }
  
  
              //add values for valid attribute
  
             if([entitySpecifier isEqual:@2]){
                  Categories *category = (Categories *)[[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
  
                  [category setValue:value forKey:attribute];
                  [self.elRehabCoreData.managedObjectContext insertObject:category];
  
              }else if ([entitySpecifier isEqual:@3]){
                  Companies *company = (Companies *)[[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
                  [company setValue:value forKey:attribute];
                  [self.elRehabCoreData.managedObjectContext insertObject:company];
              }
  
  
          }
  
      }
  }
  </code></pre>
  
  <p>And the Data Model looks like this: 
  <img src="https://i.stack.imgur.com/goYEQ.png" alt="enter image description here"></p>