block
2015-09-16 10:44
316 查看
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];
}];
}];
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];
}];
}];
相关文章推荐
- 从谷歌说开去:互联网的未来将充满垄断
- KEIL MDK 查看代码量、RAM使用情况--RO-data、RW-data、ZI-data的解释(转)
- 轻乳酪蛋糕
- C# 系统应用之TreeView控件显示树状磁盘文件目录及加载图标
- 直接启动SDK Manager: $ADNROID_HOME/tools/android
- hdu 5442 Favorite Donut(后缀数组)
- 常量指针和指针常量
- spark调度系列------2. Spark Executor的创建和启动过程
- 不安装oracle客户端,用plsql连接oracle
- CodeForcesGym 100735H Words from cubes
- (大数据分析-3)HBASE Review
- 点滴感悟
- ios开发问题2—uitableviewController中如何改变tableview的位置
- 使用脚本便捷地在 Ubuntu 中安装最新 Linux 内核
- js 闭包概念
- cisco配置在线下载工具
- PHP Ajax 跨域问题最佳解决方案
- 君乐宝换奶粉方法
- android Service(一)
- 布局——线性布局、相对布局