核心数据视图控制器出错

时间:2012-02-19 13:48:47

标签: objective-c ios core-data

我有一个核心数据堆栈,其中有两个实体:'Client'和'Car'。两者都由tableViewControllers表示。

第一个tableViewController获取客户端列表,然后一旦选中,第二个显示 客户端拥有的汽车列表。两者都被推到导航控制器上。当我从第二个视图控制器“返回”时,程序成功显示firstviewcontroller,等待一秒左右然后崩溃。当我进行“构建和调试”时,控制台发出了这个错误:

Program received signal:  “EXC_BAD_ACCESS”.

我不明白。我应该在哪里找到错误?

编辑:我在下面列出了一些代码,看看是否是由于内存处理不当...我删除了所有注释掉的方法,以及错误发生前未使用的方法。

这是我的ClientListViewController ...

@implementation ClientListViewController

@synthesize clientsArray;
@synthesize coreDataModel;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];

    // Set the title
    self.title=@"Clients";

    [self populateTable];
}

-(void)populateTable {

    [self setClientsArray:[coreDataModel retrieveClientList]];

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Override to allow orientations other than the default portrait orientation.
    return YES;
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [clientsArray count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...

    Client *client = (Client *)[clientsArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [client name];

    return cell;

    [client release];


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // Create and push new view controller.
    ClientCarsViewController *clientCarsViewController = [[ClientCarsViewController alloc] initWithNibName:@"ClientCarsViewController" bundle:nil];

    //Pass the CoreDataModel to the view controller
    clientCarsViewController.coreDataModel = coreDataModel;

    // Pass the selected object to the new view controller
    Client *client = (Client *)[clientsArray objectAtIndex:indexPath.row];
    clientCarsViewController.client = client;

    // Push the new viewController
    [self.navigationController pushViewController:clientCarsViewController animated:YES];

    // Release the objects
    [clientCarsViewController release];
    [client release];

}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    self.clientsArray = nil;
}


- (void)dealloc {

    [clientsArray release];
    [coreDataModel release];
    [super dealloc];

}


@end

这是我的ClientCarsViewController实现......

@implementation ClientCarsViewController

@synthesize carsArray;
@synthesize coreDataModel;
@synthesize client;


#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = client.name;

    // Get client's cars
    NSSet *cars = client.cars;

    // Import them into the carsArray
    [self setCarsArray: [NSMutableArray arrayWithArray:[cars allObjects]]];

    [cars release];

}

-(void)addCarToClient {

    [coreDataModel addCarToClient:(Client *)client];

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Override to allow orientations other than the default portrait orientation.
    return YES;
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [carsArray count];

}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    Car *car = (Car *)[carsArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [car carName];
    return cell;

    [car release];

}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.

}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    self.carsArray = nil;
}


- (void)dealloc {

    [self.client release];
    [self.coreDataModel release];
    [self.carsArray release];
    [super dealloc];
}


@end

1 个答案:

答案 0 :(得分:1)

您正在发布您不拥有的对象。看看Objective-C Memory Management Rules

例如,当您获得这样的对象client时:

Client *client = (Client *)[clientsArray objectAtIndex:indexPath.row];

你不拥有它,不应该发布它。