title: How do I add an NSManagedObject to Core Data and specify it's "parent" object? (iPhone)
body: <p>Okay, here's the situation.  I have two <code>NSManagedObjects</code> called <code>Store</code> and <code>Aisle</code>.  There is a one-to-many relationship from <code>Store</code> to <code>Aisle</code> and a one-to-one relationship from <code>Aisle</code> to <code>Store</code>.</p>
  
  <p>I have existing stores added in Core Data that I can access just fine.  My problem is that I cannot figure out how to add an <code>Aisle</code> object to an existing <code>Store</code>.  I've tried fetching the <code>Store</code> and then adding an <code>Aisle</code> object to the <code>.Aisles</code> set, I've tried using the <code>AddAislesObject:</code> method provided by CoreData in <code>Store.h</code>, and I've tried adding a <code>Store</code> object to the <code>.Stores</code> set in the <code>Aisle</code> object.</p>
  
  <p>I'm watching my SQLite database and it looks like the <code>Aisles</code> are being added successfully and that they are associated to the correct <code>Store</code> using the Core Data primary key.  But for some reason when I try to retrieve the <code>Aisle</code> objects (using the <code>.Aisles</code> property of the fetched <code>Store</code> object) I always get a zero-count <code>NSSet</code>.</p>
  
  <p>I've also tried deleting my .sqlite file to no avail.</p>
  
  <p>Does anyone have some simple code or even a high level suggestion for the best way to do this?</p>
  
  <p>Thank you!</p>
  
  <p>--- UPDATE 8/30 0917 CST</p>
  
  <p>I've taken some of Adam's suggestions below and my aisles are definitely saving into Core Data correctly.  However, when I try to access the saved Aisle from the Store object, I'm not getting anything returned.  In the code below you can see that I'm retrieving an existing Store object from Core Data using a name and location (there are safeguards on insert to make sure that this combination is unique).  I then use some NSLog debugs to access the single Store in the returned array, and the aisles property to iterate through any aisles. Ignore everything below the debug loops because I haven't finished the method yet.</p>
  
  <p>What I'm seeing is that aisleList has zero objects in it.</p>
  
  <pre><code>-(NSArray *)getAisleListByStore: (Store *)aStore {
  
  // this method should return an NSArray of aisles for aStore
  
  NSManagedObjectContext *context = [self managedObjectContext];
  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  NSEntityDescription *entity = [NSEntityDescription entityForName:@"Store" inManagedObjectContext:context];
  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name MATCHES[c] %@ AND location MATCHES[c] %@", aStore.name, aStore.location];
  
  [fetchRequest setEntity:entity];
  [fetchRequest setPredicate:predicate];
  
  NSError *error;
  NSArray *storesList = [context executeFetchRequest:fetchRequest error:&amp;error];
  [fetchRequest release];
  
  if (storesList == nil || [storesList count] != 1) {
  
      // error detected
      NSLog(@"Error retrieving stores: %@", [error localizedDescription]);
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error retrieving stores" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
      [alert show];
      [alert release];
      return nil;
  
  } else {
  
      // get aisles
  
      // debugs
      for (Store *theStore in storesList) {
          NSLog(@"Name: %@", theStore.name);
          NSLog(@"Aisle count: %d", [theStore.aisles count]);
          NSSet *aisleList = theStore.aisles;
  
          for (Aisle *theAisle in aisleList) {
              NSLog(@"Aisle name: %@", theAisle.name);
          }
      }
  
      // Store *theStore = [storesList objectAtIndex:0];
  
      // NSArray *aisles = (NSArray *)theStore.aisles;
      // return aisles;
  }
  </code></pre>
  
  <p>}</p>