views:

262

answers:

3

When the following code is run on the iPhone the count of fetched objects after the delete is one less than before the delete. But on the iPad the count remains the same. This inconsistency was causing a crash on the iPad because elsewhere in the code, soon after the delete, fetchedObjects is called and the calling code, trusting the count, attempts access to the just-deleted object's properties, resulting in a NSObjectInaccessibleException error (see below). A fix has been to use that commented-out call to performFetch, which when executed makes the second call to fetchObjects yield the same result as on the iPhone without it. My question is: Why is the iPad producing different results than the iPhone? This is the second of these differences that I've discovered and posted recently.

-(NSError*)deleteObject:(NSManagedObject*)mo;
{
NSLog(@"\n\nNum objects in store before delete: %i\n\n",
      [[self.fetchedResultsController fetchedObjects] count]);

    [self.managedObjectContext deleteObject:mo];

    // Save the context.
    NSError *error = nil;
    if (![self.managedObjectContext save:&error]) {
    }

//  [self.fetchedResultsController performFetch:&error];  // force a fetch

NSLog(@"\n\nNum objects in store after delete (and save): %i\n\n", 
      [[self.fetchedResultsController fetchedObjects] count]);

    return error;
}

(The full NSObjectInaccessibleException is: "Terminating app due to uncaught 
 exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault 
 for '0x1dcf90 <x-coredata://DC02B10D-555A-4AB8-8BC4-F419C4982794/Blueprint/p"
+2  A: 

Adding the following code to your FRC delegate will resolve this.

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { }

Thanks to BenT at the Apple Dev forum for the answer (see my comments above). I asked him for an explanation of the fix and he said, "The iPad is using iPhoneOS 3.2 while the iPhones 3.1. There were a number of improvements to the NSFetchedResultsController in 3.2, but unfortunately, a side effect on the delegates' requirements to actually implement one (any one) of the delegate methods to get active change tracking." https://devforums.apple.com/message/221471#221471 (Hope this helps someone. The faq said it was ok to answer your own question in such cases)

Alyoshak
This was driving me absolutely insane. This exact same issue and fix also applies to iOS 4.0 (iPhone OS 4.0). I just wish I had found this post 6 hours ago.
Chris
A: 

Do keep in mind that the fetchedresultscontroller caches data to speed up its work. After deleting an object, you could also call + (void)deleteCacheWithName: to make sure the deleted object won't linger in the cache. That is, if you're not using delegate calls.

Adriaan
A: 

To add a delegate method did not solve my problem. But I solved it by adding a forced performFetch after the deletion was finished. I also delete the cache before performing the fetch.

I'm on iOS 4.1.

hinderberg