I’m getting a weird zombie object issue when I’m trying to merge my changes from a background NSOperation:
(controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:]: message sent to deallocated instance)
I have a ViewController that pushes other controllers on a navController stack in my AppDelegate in the didSelectRowForIndexPath like so:
ABCViewController *myVC = [[ABCViewController alloc] initWithNibName:@"ABCViewController" bundle:nil];
ABCEvent *selectedEvent = [_fetchedResultsController objectAtIndexPath:indexPath];
[myVC setManagedObjectContext:[self managedObjectContext]];
[myVC setTitle:@"Title"];
[myVC setEvent:selectedEvent];
ABCAppDelegate *appDelegate = (ABCAppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate navController] pushViewController:myVC animated:YES];
[myVC release];
Then in my ViewDidLoad for ABCViewContoller I’m creating an operation queue, and adding my background operation to it:
_operationQueue = [[NSOperationQueue alloc] init];
As well as hooking up a notification so I can merge the changes:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contextDidSave:)
name:NSManagedObjectContextDidSaveNotification
object:nil];
All standard stuff I believe. The issue is if I select the row in the main VC, then go back to it, and hit the row again, I get that: message sent to deallocated instance message. Now, if I turn off the notification for NSManagedObjectContextDidSaveNotification, then I don’t get the error, so that’s definitely the culprit. My dealloc in my second controller is like this:
- (void)dealloc
{
[super dealloc];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_operationQueue release];
}
So I’m unhooking that class as an observer. In my mind it shouldn’t be trying to merge changes on any unallocated instances. I believe somehow though it’s trying to merge changes back in on the first instance of my controller when I touch the row again, even though I stopped subscribing to that notification.
I’m pretty stumped on this. Any help would be much appreciated.
Are you using a fetchedResultsControllerDelegate at all?
If you are, make sure to also set that to nil when the view disappears or unloads, otherwise it will continue to receive messages about changes in your data, regardless of removing the observer.