title: Core Data, One-To-Many Relationship
body: <p>I new to iOS development and doing some test with the core data. It was all fine until I started playing with relationship.
  In context:</p>
  
  <p>I have 2 entity: Article and Category.</p>
  
  <p>Article has two members idArticle and text. Articles has to have a single Category.
  Category has two members idCategory and name. Category can have 0 or several Articles.</p>
  
  <p>Article:</p>
  
  <ul>
  <li>idArticle</li>
  <li>text</li>
  <li>category (Relationship to Category, Inverse = articles, minimum optional, maximum 1)</li>
  </ul>
  
  <p>Category:</p>
  
  <ul>
  <li>idCategory</li>
  <li>name</li>
  <li>articles (To many Relationship to Article, Inverse = category, minimum optional, maximum unlimited)</li>
  </ul>
  
  <p>While I am adding an article. I am first surprise to not only have article1.category but also article1.idCategory and article1.name! 
  I have currently set all my attributes, relationship to optional. 
  Whatever I do, when I add a new article using the code below, it will add a new Category as well which will contain idCategory = 0 and name = nil if I dont set article.idCategory and article.name! or to the corresponding value if I set them. However, I don't want it to create that category, I just want to add an existing category. 
  article.category works fine; it add the article to the right category! If I only set article.category; article.idCategory will = 0 and article.name = nil.</p>
  
  <p>I suppose that I could delete the newly created category but I want my code to be neat. I have search the web but didnt find similar example with that problem. I am not dealing with any GUI here.
  My code:</p>
  
  <pre><code> - (BOOL)createNewGmtArticle:(NSNumber*)articleID title:(NSString*)paramTitle text:(NSString*)paramText date:(NSDate*)paramDate categoryID:(NSNumber*)paramCategoryID categoryName:(NSString*)paramCategoryName
  
  {
      GasMattersTodayArticles *gmtArticle = [NSEntityDescription insertNewObjectForEntityForName:@"GasMattersTodayArticles" inManagedObjectContext:self.managedObjectContext]; // Look the given entitiy GasMattersTodayArticles in the given managed obj context
  
      if(gmtArticle != nil)
      {
          // Fill article
          gmtArticle.idArticle = articleID;
          gmtArticle.text = paramText;
  
          gmtArticle.category = [self getGmtCategoryWithId:paramCategoryID];
          //gmtArticle.idCategory = gmtArticle.category.idCategory; 
          //gmtArticle.name = gmtArticle.category.name;        
  
          NSError *savingError = nil;
          if([self.managedObjectContext save:&amp;savingError]) // flush all unsaved data of the context to the persistent store
          {
              NSLog(@"Successfully saved the context");
              return YES;
          }
          else
          {
              NSLog(@"Failed to save the context. Error = %@", savingError);
              return NO;
          }
      }
  
      NSLog(@"Failed to create the new article");
      return NO;
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>-(GasMattersTodayCategory *)getGmtCategoryWithId:(NSNumber*)categoryID
  
  {
      // Create the fetch request first
      NSDictionary *subs = [NSDictionary dictionaryWithObject:categoryID forKey:@"SEARCH_KEY"];    
      NSFetchRequest *fetchRequest = [self.managedObjectModel fetchRequestFromTemplateWithName:@"CategoryWithKey" substitutionVariables:subs];
      // Entity whose contents we want to read
      NSEntityDescription *entity = [NSEntityDescription entityForName:@"GasMattersTodayCategory" inManagedObjectContext:self.managedObjectContext];
  
      // Tell the request that we want to read the content of the person entity
      [fetchRequest setEntity:entity];
  
      // Excecute the fetch request on the context
      NSError* requestError = nil;
      GasMattersTodayCategory *category = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&amp;requestError] lastObject];
  
      // Make sur we get a category
      if(category != nil)
      {
          return category;
      }
      else
      {
          NSLog(@"Could not find any Category entities with this Id in the context.");
          return nil;
      }    
  }
  </code></pre>
  
  <p>Thanks! This super basic task is currently ruining my sunday!</p>