iPhone iOS 4 Core Data - Program received signal: “EXC_BAD_ACCESS”
I'm trying to create an app based on Apple's example project TheElements, but using Core Data for the model. In Core Data I have 4 related DB tables. In the UI I have several tableViews, each showing rows from a different Db table. Clicking a row in a tableView drills down to items in a related table, shown in another TableView.
Everything is working but the app crashes unexpectedly at random times with the error: Program received signal: “EXC_BAD_ACCESS”. BTW this error only shows in the console when debugging on the device. No error shows when debugging on the simulator. This screen grab shows the contents of the debugger after a crash.
I have no idea how to decipher the debugger. All I can see is the crash seems to stem from the main() function and _PFManagedObjectReferenceQueue is also listed, which leads to guess that I'm doing something wrong with Core Data.
To implement Core Data I'm adding the following to my App Delegate header:
@private
NSManagedObjectContext *managedObjectContext_;
NSManagedObjectModel *managedObjectModel_;
NSPersistentStoreCoordinator *persistentStoreCoordinator_;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator
and adding Apple's default methods for these to App Delegate implementation file.
Then to my Data Source Protocol header I've added:
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;`
and my Data Source header f开发者_开发技巧iles are as follows:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "TableViewDataSourceProtocol.h"
@interface MatchesAllDataSource : NSObject <UITableViewDataSource,TableViewDataSource, NSFetchedResultsControllerDelegate>
{
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
}
@end
When a table cell is clicked I pass the selectedObject as follows:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath
{
// deselect the new row using animation
[tableView deselectRowAtIndexPath:newIndexPath animated:YES];
// get the element that is represented by the selected row.
Match *selectedMatch = [dataSource objectForIndexPath:newIndexPath];
// create an AtomicElementViewController. This controller will display the full size tile for the element
MatchViewController *matchController = [[MatchViewController alloc] init];
// set the element for the controller
matchController.selectedMatch = selectedMatch;
// push the element view controller onto the navigation stack to display it
[[self navigationController] pushViewController:matchController animated:YES];
[matchController release];
}`
Does anyone have any idea what might be causing my crash? Will someone please point me in the right direction to look for an answer?
Is there a better way to implement Core Data with multiple tableViews? Will someone point me to a good example of Core Data with multiple tableViews?I had this problem too with Marcus' code because I changed the UITableViewController to a regular UIViewController and forgot to put in the title. Here is his original code:
- (id)initWithStyle:(UITableViewStyle)style {
if (self = [super initWithStyle:style]) {
self.title = @"Australia";
}
return self;
}
So if you happened to have changed things make sure that you are setting the self.title somewhere so the custom NSArray+PerformSelector can see it when the app is initializing... like in init.
I had this a few days ago. In my case, I was accessing an object that had already been deallocated.
Also I passing the managedObjectContext object from view to view as the user navigates down the navigationController views like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath
{
matchSegmentedController.selectedMatch = selectedMatch;
matchSegmentedController.managedObjectContext = dataSource.managedObjectContext;
精彩评论