title: "Dangling reference to an invalid object" error in Core Data with seemingly valid objects
body: <p>I'm having a really confounding problem using Core Data.  In my Core Data store, for an existing Core Data object, I'm checking whether a relationship exists, and if not, I create the object like so (this is a method on AFFingerprintGeneratorOperation):</p>
  
  <pre><code>NSManagedObjectContext *newContext = [[NSManagedObjectContext alloc] init];
  [newContext setMergePolicy:NSOverwriteMergePolicy];
  NSPersistentStoreCoordinator *sharedStoreCoordinator = [[AFMainController sharedInstance] persistentStoreCoordinator];
  [newContext setPersistentStoreCoordinator:sharedStoreCoordinator];
  [self setManagedObjectContext:newContext];  // retaining property
  [newContext release];
  
  NSEntityDescription *fetchedTagSetEntity = [NSEntityDescription entityForName:@"FetchedTagSet"
                                                         inManagedObjectContext:newContext
                                              ];
  
  AFTrack *retrievedTrack = (AFTrack *)[newContext objectWithID:[self trackObjectID]];
  [self setTrack:retrievedTrack];
  
  if (! retrievedTrack.fetchedTagSet) {
      AFFetchedTagSet *newFetchedTagSet = [[AFFetchedTagSet alloc] initWithEntity:fetchedTagSetEntity
                                                   insertIntoManagedObjectContext:newContext];
  
      [[retrievedTrack storedTrack] setFetchedTagSet:newFetchedTagSet];
      [newFetchedTagSet setStoredTrack:[retrievedTrack storedTrack]];
  }
  </code></pre>
  
  <p>AFTrack, AFFetchedTagSet, and AFStoredTrack are all Core Data objects.  AFFetchedTagSet and AFStoredTrack are in an on-disk Core Data store, while AFTrack is in a separate in-memory Core Data store.</p>
  
  <p>Note that since the AFStoredTrack object is in a separate store, I need to fetch it like so (this is a method on AFTrack):</p>
  
  <pre><code>NSManagedObjectContext *objectContext = [self managedObjectContext];
  NSPersistentStoreCoordinator *coordinator = [objectContext persistentStoreCoordinator];
  NSString *URIString = [self storedTrackObjectIDString];
  
  AFStoredTrack *theStoredTrack = nil;
  if (URIString) {
      NSURL *objectURL = [NSURL URLWithString:URIString];
      NSManagedObjectID *storedTrackObjectID = [coordinator managedObjectIDForURIRepresentation:objectURL];
      theStoredTrack = (AFStoredTrack *)[objectContext objectWithID:storedTrackObjectID];
  }
  
  return theStoredTrack;
  </code></pre>
  
  <p>Since this is a method on AFTrack, it simply retrieves the AFTrack's own managed object context, so the code in the first excerpt should always use the exact same managed object context for all operations.</p>
  
  <p>However, in the first excerpt, after calling setFetchedTagSet: with the new object, and attempting to save the Core Data store, I get this error:</p>
  
  <pre><code>"Dangling reference to an invalid object." = "&lt;null&gt;";
  NSAffectedObjectsErrorKey =     (
      "&lt;AFStoredTrack: 0x11550eef0&gt; (entity: StoredTrack; id: 0x101eb57d0 &lt;x-coredata:///StoredTrack/tF7F5568E-2959-4786-B73D-B7AC6586F5B9121&gt; ; data: {\n    fetchedTagSet = \"0x11c956b60 &lt;x-coredata:///FetchedTagSet/tF7F5568E-2959-4786-B73D-B7AC6586F5B9124&gt;\";\n    fingerprint = \"ASPtPiNHPC7fGSYXTxtfFboMCg7BCxYQ+gZRCL4FWQdzBD8HPw\";\n    persistentID = nil;\n    status = 3;\n    updateAlbumName = nil;\n    updateArtistName = nil;\n    updateArtwork = nil;\n    updateGenre = nil;\n    updateLyrics = nil;\n    updateReleaseYear = nil;\n    updateTrackName = nil;\n})"
  );
  NSLocalizedDescription = "storedTrack is not valid.";
  NSValidationErrorKey = storedTrack;
  NSValidationErrorObject = "&lt;AFFetchedTagSet: 0x11c956ac0&gt; (entity: FetchedTagSet; id: 0x11c956b60 &lt;x-coredata:///FetchedTagSet/tF7F5568E-2959-4786-B73D-B7AC6586F5B9124&gt; ; data: {\n    ampliFindPUID = nil;\n    fingerprint = nil;\n    matchAlbum = nil;\n    matchLyrics = nil;\n    matchTrackName = nil;\n    matchTrackNumber = 0;\n    storedTrack = \"0x101eb57d0 &lt;x-coredata:///StoredTrack/tF7F5568E-2959-4786-B73D-B7AC6586F5B9121&gt;\";\n})";
  NSValidationErrorValue = "&lt;AFStoredTrack: 0x11550eef0&gt; (entity: StoredTrack; id: 0x101eb57d0 &lt;x-coredata:///StoredTrack/tF7F5568E-2959-4786-B73D-B7AC6586F5B9121&gt; ; data: {\n    fetchedTagSet = \"0x11c956b60 &lt;x-coredata:///FetchedTagSet/tF7F5568E-2959-4786-B73D-B7AC6586F5B9124&gt;\";\n    fingerprint = \"ASPtPiNHPC7fGSYXTxtfFboMCg7BCxYQ+gZRCL4FWQdzBD8HPw\";\n    persistentID = nil;\n    status = 3;\n    updateAlbumName = nil;\n    updateArtistName = nil;\n    updateArtwork = nil;\n    updateGenre = nil;\n    updateLyrics = nil;\n    updateReleaseYear = nil;\n    updateTrackName = nil;\n})";
  </code></pre>
  
  <p>But both the AFFetchedTagSet and AFStoredTrack objects seem to be valid, as their ids match up, and the object context for these objects still exists and is retained by the AFFingerprintGeneratorOperation object.</p>
  
  <p>I've seen this <a href="https://stackoverflow.com/questions/2003648/coredata-dangling-reference-to-an-invalid-object-error">CoreData: &quot;Dangling reference to an invalid object.&quot; error</a> and this <a href="http://lists.apple.com/archives/cocoa-dev/2009/Nov/msg00190.html" rel="nofollow noreferrer">http://lists.apple.com/archives/cocoa-dev/2009/Nov/msg00190.html</a> , but neither link seems to help.  The former seems to say that a relationship is bad (which I don't think it is), and the latter says to avoid changing relationships in awakeFromFetch, which as far as I can tell I'm not doing.</p>
  
  <p>Any help?</p>