I am presenting a table view in modal view controller created with the following code (http://stackoverflow.com/questions/4471289/how-to-filter-nsfetchedresultscontroller-coredata-with-uisearchdisplaycontroll) so I can use Core Data, NSFetchedResults and a UISearchbar. I finally have everything working well on the table view/search/detail view end, but I just discovered a showstopper I can’t seem to figure out.
When I dismiss the tableview using:
- (IBAction)doneButtonPressed:(id)sender {[self dismissModalViewControllerAnimated:YES];}
the modal view dismisses, the main view loads, and then the app crashes. It’s getting far enough that I can call this nslog from the main view controller:
-(void) viewDidAppear:(BOOL)animated { NSLog(@"do I crash");}
I’m getting the bad access error here:
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));}}
I believe the issues may be arc related. The code template I used was not for arc, so I made the following changes:
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSFetchedResultsController *fetchedResultsController;
to
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong, readonly) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSFetchedResultsController *searchFetchedResultsController;
@property (nonatomic, retain) UISearchDisplayController *mySearchDisplayController;
to
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, strong) NSFetchedResultsController *searchFetchedResultsController;
@property (nonatomic, strong) UISearchDisplayController *mySearchDisplayController;
In addition, I removed the following from didreceivememorywarning:
[fetchedResultsController_ release];
[searchFetchedResultsController_ release];
and changed the following in – (NSFetchedResultsController *)fetchedResultsController
return [[fetchedResultsController_ retain] autorelease];
to
return fetchedResultsController;
and in – (NSFetchedResultsController *)searchFetchedResultsController
return [[searchFetchedResultsController_ retain] autorelease];
to
return searchFetchedResultsController;
I am completely stumped. Any assistance would be greatly appreciated!
Well, problem solved. I can’t imagine I’m the first or last to have this issue, so in case you are having similar difficulties….
After scouring the search code (I figured it had to have something to do with that, since if I searched before I closed the modal view, it wouldn’t crash), I still couldn’t find anything wrong.
I realized, however, that I was creating both my searchBar and searchDisplayDelegate programmatically. On a whim, I decided to create the searchBar and displayDelegate in storyboard and then link them up. Once they were added, I commented out from loadView:
In addition, adding them in storyboard autocreated the following in viewDidUnload:
Viola! No more crash… I think it was simply the mySearchDisplayController not unloading properly. Hope that helps others!