繁体   English   中英

使用iOS 5 ARC发布对象的时间过早

[英]Object Released too soon with iOS 5 ARC

我是Objective-C编程语言的新手,我注意到内存管理中有一个奇怪的行为。 默认情况下,iOS 5启用了ARC,但是对象发布太早,也许有人可以给我一些建议。

就是这种情况:

我有一个Dao对象,可以使用Core Data PersistenceStore从SQLite数据库检索数据。 此Dao实现方法“ getAllAuthors”

在我的viewController的viewWillAppear方法中,我加载此数据并加载数据,但是当我填充tableView时,对象为(空)。

我发布一些代码:

@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。 有什么线索吗?

提前致谢

dao = [[CrossoverDao alloc] init];
authors = [NSArray arrayWithArray:[dao getAllAuthors]];
[authorsTable reloadData];
NSLog(@"author in viewWillAppear: %@", [[authors objectAtIndex:0] name]);

这应该在控制器的viewDidLoad或loadView方法中完成。 在viewWillAppear中,您通常只要求更新UI。

您应该确保使用综合访问器设置属性,以便ARC知道如果它们很强就想保留它们(此处只是在viewWillAppear方法的末尾插入了发行版)。 更改

dao = [[CrossoverDao alloc] init];
authors = [NSArray arrayWithArray:[dao getAllAuthors]];

self.dao = [[CrossoverDao alloc] init];
self.authors = [NSArray arrayWithArray:[dao getAllAuthors]];

我没有使用过ios5 ARC,但是也许您可以尝试保留在viewWillAppear中并在viewWillDisappear中发布吗?

[authors retain];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM