First of all, I am working on an app on the iPad that displays Twitter tweets on a map in core data. Initially, when the view loads for the master view controller, I initialize the fetchResultsController and fetch the Tweets from core data on a different thread using the code below:
- (void)viewDidLoad
{
[super viewDidLoad];
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
// Creates fetchedResultsController and performs fetch
[self initializeFetchResultsController];
self.fetchedResultsController.delegate = self;
[self performFetchOnDifferentQueue];
}
// Performs the fetch on a different queue
- (void)performFetchOnDifferentQueue
{
dispatch_queue_t fetchQ = dispatch_queue_create("fetchQ", NULL);
dispatch_async(fetchQ, ^{
[self performFetch];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
[self setTopTweetsOnMap];
});
});
}
// Sets the Top Tweets on Map by assigning tweets into
// the detail view controller's array
- (void)setTopTweetsOnMap
{
NSUInteger maxRange = [[self.fetchedResultsController fetchedObjects] count] > 100 ? 100 : [[self.fetchedResultsController fetchedObjects] count];
self.detailViewController.topTweets = [[self.fetchedResultsController fetchedObjects] objectsAtIndexes:[[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, maxRange)]];
}
The method (void)setTopTweetsOnMap
sets the array topTweets which is a property that is part of the public api on the detail view controller. The public api also consists of a mapview. The code below is the part of the implementation of the map view to display Tweets on a map:
// Makes view the delegate
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
}
// When top Tweets are set, we add annotations for them
- (void)setTopTweets:(NSArray *)topTweets
{
_topTweets = topTweets;
[self.mapView removeAnnotations:self.mapView.annotations];
[self.mapView addAnnotations:self.topTweets];
}
Now everything works fine until the view loads, but I have a segmented control that allows the fetchedResultsController to sort the Tweets according to newest or oldest. Below is the code for the target-action code:
// Action determines whether to sort Tweets by newest or oldest
- (IBAction)sortTweetsChanged:(UISegmentedControl *)sender
{
[NSFetchedResultsController deleteCacheWithName:self.fetchedResultsController.cacheName];
BOOL ascending = sender.selectedSegmentIndex == 0 ? NO : YES;
self.fetchedResultsController.fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"timestamp"
ascending:ascending]];
[self performFetchOnDifferentQueue];
}
Basically, this code does the change to the NSFetchedResultsController according to the apple documentation and updates the topTweets on the detail view controller to display the new Tweets. The first time I do the sorting everything seems fine, but when click on the UISegmentedControl to sort again I get this message:
An instance 0x1ac11330 of class Tweet_Tweet_ was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
So I looked around stack overflow about the message and the only help I am getting is that the message have to do with observers. However, what I don't understand is why I am getting the message since I do not explicitly have any observing things in my program. I have looked into putting a break point at the NSKVODeallocateBreak and printing out the instance, but I simply get a Tweet instance in core data and don't know what to do after.
I have also tried experimenting with setting the mapView.delegate
to nil at various places but have had no luck with that. There is obviously more code to this, but I'm sure that those wouldn't matter since the master view controller works fine without the line [self setTopTweetsOnMap]
. I believe that the problem exists with the map view and annotations but don't know what to do.