title: Core Data - relationship goes nil unexpectedly
body: <p>I am struggling with some bizarre behavior in Core Data. I have a fairly standard set-up, using the CoreDataBook example: I have a RootView which is using a NSFetchedResultsController to display list of Items. Item has a few attributes and relationships to other entities. I have a DetailView which I use to create a new Item, as well as edit an existing Item, which I present modally. In DetailView: viewDidLoad, I create a new managedObjectContext in which I want to make all the changes ... if the user presses Save, I save this context and merge the changes back; otherwise, if the user presses cancel, all those changes just disappear. </p>
  
  <p>The "add new Item" part of this works fine, but when I select the row to bring up the same DetailView which an existing Item, one of the relationships (which shows fine in the RootView in the debugger) suddenly becomes nil when it's presented in the DetailView. Here's the code to display the DetailView in UITableView's didSelectRowAtIndexPath: </p>
  
  <pre><code>    Item *managedObject = (Item *)[self.fetchedResultsController objectAtIndexPath:indexPath];
      DetailView *childController = [[DetailView alloc] initWithNibName:@"DetailView" bundle:nil];
  
      UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:childController];    
  
      childController.existingItem = managedObject;
  
      // ** Item's relationship to Title is not nil at this point
      [self presentModalViewController:navController animated:YES];
      // ** Item's relationship Title is now nil
  
      [childController release];
      [navController release];
  </code></pre>
  
  <p>There's nothing extra-ordinary in the DetailView controller that would cause this. In fact, it doesn't even have a chance to really to any damage ... as soon as it starts, the existingItem.title relationship is already nil. [existingItem is a retained property of DetailView]</p>
  
  <p>Any idea where I should start looking into this? It's been driving me crazy for the past few hours. Here's some code in DetailView viewDidLoad, but the relationship is nil even before it's called:</p>
  
  <pre><code>    // Create a new managed object context
      NSManagedObjectContext *addingContext = [[NSManagedObjectContext alloc] init];
      self.addEditContext = addingContext;
      [addingContext release];
      [self.addEditContext setPersistentStoreCoordinator:[[appDelegate managedObjectContext] persistentStoreCoordinator]];
  
      if (!self.existingItem) {
          self.existingItem = [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext: self.addEditContext];
  
      }else{
          self.existingItem = (Item *)[self.addEditContext objectWithID:[self.existingItem  objectID]];
      }
  </code></pre>
  
  <p>The title relationship is set by selecting from some list:</p>
  
  <pre><code>self.existingItem.title = selectedTitle;
  </code></pre>
  
  <p>In the save: method, I save the addEditContext and merge changes with the appdelegate context:</p>
  
  <pre><code>    NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
      [dnc addObserver:self selector:@selector(addControllerContextDidSave:) name:NSManagedObjectContextDidSaveNotification object: self.addEditContext];
  
      // Save the context.
      NSError *error = nil;
      if (![self.addEditContext save:&amp;error]){
          NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
      }
  
      [dnc removeObserver:self name:NSManagedObjectContextDidSaveNotification object:self.addEditContext];
      self.addEditContext = nil;
  </code></pre>
  
  <p>In addControllerContextDidSave:</p>
  
  <pre><code>- (void)addControllerContextDidSave:(NSNotification*)saveNotification {
      id appDelegate = [[UIApplication sharedApplication] delegate];
      // Merging changes causes the fetched results controller to update its results
      [[appDelegate managedObjectContext] mergeChangesFromContextDidSaveNotification:saveNotification];   
  }
  </code></pre>
  
  <p>So the save: stuff works fine for a new Item, but when that Item is set to existingItem and loaded again, the self.existingItem.title is nil. And it's nil from the point where it's presented in the view controller (even though it's not nil before it's presented). So in the main context, it loads the Item and it's title relationship fine, but then title suddenly disappears when it's presented in presentModalViewController:navController. </p>
  
  <p>Really strange. If anyone can shed some light on this, it would be really appreciated. </p>
  
  <p>Update: another thing to mention is that the title is definitely persisted in the persistentStore. Every time I close and reload the app, the RootView shows the title relationship being set. As soon as I select the row, the relationship becomes nil. </p>