我是Objective-c编程语言的新手,我注意到内存管理中有一种奇怪的行为。 使用iOS 5 ARC默认启用但是对象过早发布,也许有人可以给我一些建议。
情况就是这样:
我有一个Dao对象,它使用Core Data PersistenceStore从SQLite数据库中检索数据。 这个Dao实现了一个方法“getAllAuthors”
在viewWillAppear方法的viewController中我加载了这个数据并加载了数据但是当我填充tableView时,对象是(null)。
我发布了一些代码:
@synthesize authors;
@synthesize authorsTable;
@synthesize dao;
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"author in viewDidLoad: %@", [[authors objectAtIndex:0] name]);
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
dao = [[CrossoverDao alloc] init];
authors = [NSArray arrayWithArray:[dao getAllAuthors]];
[authorsTable reloadData];
NSLog(@"author in viewWillAppear: %@", [[authors objectAtIndex:0] name]);
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[authorsTable reloadData];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSLog(@"author in viewWillDisappear: %@", [[authors objectAtIndex:0] name]);
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
NSLog(@"author in viewDidDisappear: %@", [[authors objectAtIndex:0] name]);
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
return YES;
}
#pragma mark -
#pragma mark UITableViewDataSource datasource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"[AuthorsViewController::cellForRowAtIndexPath] Constructing row at indexPath: %d", indexPath.row);
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if(indexPath.section == 0)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
else
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[authors objectAtIndex:indexPath.row] name];
cell.detailTextLabel.text = [[authors objectAtIndex:indexPath.row] bio];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section {
NSLog(@"author in numberOfRowsInSection: %@", [[authors objectAtIndex:0] name]);
return [authors count];
}
在cellForRowAtIndexPath中,作者为null。任何线索?
提前致谢
答案 0 :(得分:1)
dao = [[CrossoverDao alloc] init];
authors = [NSArray arrayWithArray:[dao getAllAuthors]];
[authorsTable reloadData];
NSLog(@"author in viewWillAppear: %@", [[authors objectAtIndex:0] name]);
这应该在控制器的viewDidLoad或loadView方法中完成。 在viewWillAppear中,您通常只是请求UI更新。
答案 1 :(得分:1)
您应该确保使用合成访问器设置属性,以便ARC知道如果它们很强大就要保留它们(这里它只是在viewWillAppear
方法的末尾插入了版本)。变化
dao = [[CrossoverDao alloc] init];
authors = [NSArray arrayWithArray:[dao getAllAuthors]];
到
self.dao = [[CrossoverDao alloc] init];
self.authors = [NSArray arrayWithArray:[dao getAllAuthors]];
答案 2 :(得分:-3)
我没有使用ios5 ARC,但也许您可以尝试保留viewWillAppear并在viewWillDisappear中发布?
[authors retain];