block会有循环引用的风险--对外部
1。用self时小心
2.借助dealloc方法,判断是否循环引用
- (void)dealloc
{
NSLog(@"8888-----");
}
解决方法 把self定义成弱引用
__weak typeof(self) weakSelf = self;
NSBlockOperation *downloadOp = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"正在下载中......");
// 1. 下载图片(二进制)
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:app.icon]];
UIImage *image = [UIImage imageWithData:data];
// 2. 将下载的数据保存到模型
if (image) {
[weakSelf.imageCache setObject:image forKey:app.icon];
// 将图片写入沙盒
[data writeToFile:[self cachePathWithUrl:app.icon] atomically:YES];
}
// 3. 将操作从操作缓冲池删除
[weakSelf.operationCache removeObjectForKey:app.icon];
// 4. 更新UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// 刷新当前行
[weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}];
}];