title: CoreData Fetch Resturns 0 Results on second use of a Predicate
body: <p>Hoping you can help with a problem having in a core data app. I've boiled it down to just the essentials:</p>
  
  <p>Here's the psudocode (real code to follow in this post):</p>
  
  <pre><code>[run a fetch request] // returns 18 objects
  [save context]        // no NSError, returns YES - success
  [run a fetch request] // returns 0 objects; request is exactly the same request as previous
  </code></pre>
  
  <p>If I don't save, both fetch requests return 18 objects. </p>
  
  <p>The fetch request is done in a separate method, so I'm sure the request is the same both times. The request uses this NSPredicate:</p>
  
  <pre><code>   NSPredicate *predicateFailsAfterSave = [NSPredicate predicateWithFormat:@"NOT (%@ IN games)", self.game]; // where THISGAME is not IN self.games
  </code></pre>
  
  <p>If I change this predicate to something simpler, like "fullName CONTAINS %@", @"Frank", then it works both times, even if I include the save command.  That predicate should return all the Guests that have not been assigned to the self.Game object. It works the first time, then returns 0 if I save.</p>
  
  <p>The model, which consists of two entities:</p>
  
  <ul>
  <li><p>"Guest", Entity. 1 Attribute (fullName, String); 1 Relationship (To Many; "games", with an inverse to Game called "guests")</p></li>
  <li><p>"Game", Entity. 0 Attributes. 1 Relationship (To Many; "guests", with an inverse to Guest called "games")</p></li>
  </ul>
  
  <p>The Flow:</p>
  
  <ol>
  <li><p>Guest Objects are created by parsing a text file when the view loads. The "fullName" value is set.</p>
  
  <p>NSArray *namesArray = // this is an array of strings</p>
  
  <pre><code>for (NSString *playerName in namesArray) {
      NSManagedObject *newPlayer = [NSEntityDescription insertNewObjectForEntityForName:@"Guest"
                                                                 inManagedObjectContext:self.managedObjectContext];
      [newPlayer setValue:playerName forKey:@"fullName"];
      NSLog(@"Created %@", [newPlayer valueForKey:@"fullName"]);
  }
  </code></pre></li>
  <li><p>At the time the Guest Objects are made, the Game object has not been instantiated. (For this entire question, there is no situation where the relationship between Game and Guest will be set.) Then the Game object is created, at which point it has no objects in its Guests relationship:</p>
  
  <p>-(void)viewWillAppear:(BOOL)animated {
      [super viewWillAppear:animated];</p>
  
  <pre><code>            [[NSNotificationCenter defaultCenter] addObserver:self
             selector:@selector(mocChanged:)
          name:NSManagedObjectContextObjectsDidChangeNotification
                                                         object:nil];
  
              _game = [NSEntityDescription insertNewObjectForEntityForName:@"Game"
                                                          inManagedObjectContext:self.managedObjectContext];
  
              // testing
              [self testFetch];
  
              /// try save
              NSError *error = nil;
              if ([self.managedObjectContext save:&amp;error] == NO) {
                  NSLog(@"Error: %s: %@", __PRETTY_FUNCTION__, error.localizedDescription);
              }
  
              // re-do fetch
              [self testFetch];
          }
  
          -(void)mocChanged:(NSNotification *)notification {
              NSLog(@"notif: %@", notification.userInfo);
          }
  </code></pre></li>
  <li><p>You can see I sent the testFetch message twice, with the "save" being in between them. If I comment-out the save, they both return the same results.  Add the save, and the latter returns 0 objects.  Here's testFetch:</p>
  
  <pre><code>    -(void)testFetch {
          NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Guest"];
          NSPredicate *testKnownGoodPredicate = [NSPredicate predicateWithFormat:@"fullName CONTAINS[cd] %@", @"wood"];
          NSPredicate *predicateFailsAfterSave = [NSPredicate predicateWithFormat:@"NOT (%@ IN games)", self.game]; // where THISGAME is not IN self.games
  
          request.predicate = predicateFailsAfterSave;
  
          NSError *error = nil;
          NSArray *guestsNotCheckedInArray = [[self.managedObjectContext executeFetchRequest:request
                                                                           error:&amp;error] mutableCopy];
          if (guestsNotCheckedInArray == nil) {
              NSLog(@"Error initializing GuestsNotCheckedIn %s: %@", __PRETTY_FUNCTION__, error.localizedDescription);
          }
  
          NSLog(@"GuestsNoCheckedIn (game: %p; context:%p): %lu", self.game, self.managedObjectContext, (long)guestsNotCheckedInArray.count);
  
      }
  </code></pre></li>
  </ol>
  
  <p>The output of the preceding code:</p>
  
  <blockquote>
    <p>GuestsNoCheckedIn (game: 0x10b54aeb0; context:0x10b526840): 18 </p>
    
    <p>notif: {
        inserted = "{(\n     (entity: Game; id: 0x10b54af10
     ; data:
    {\n    guests =     (\n    );\n})\n)}";
        managedObjectContext = ""; } </p>
    
    <p>GuestsNoChecked In (game: 0x10b54aeb0; context:0x10b526840): 0</p>
  </blockquote>
  
  <p>Expected output:</p>
  
  <p>GuestsNoCheckedIn ... : 18 both times.  </p>
  
  <p>I've been working from three theories:</p>
  
  <ol>
  <li>The relationship is being impacted during Save.</li>
  <li>There's something wrong in the predicate that causes it to fail a second time.</li>
  <li>When the objects are fetched, their relationship is being altered unexpectedly. </li>
  </ol>
  
  <p>To assist with testing those theories, I've:</p>
  
  <ol>
  <li>Removed the NSManagedObject subclasses from the project. This assures me there is no -awakeFrom* that might be corrupting the relationships.</li>
  <li>Removed all other Entities from the Model.</li>
  <li>Registered to receive NSMAnagedObjectContextObjectsDidChangeNotification notifications, so I can see if something has changed that I wasn't expecting.</li>
  <li>I've compared the addresses of the MOC and Game objects to ensure I wasn't accidentally creating another one.</li>
  </ol>
  
  <p>This makes me think the problem is with the predicate, as opposed to with the save. Though I'm at a bit of a loss.  </p>
  
  <p>Any ideas?</p>