block

来源:互联网 发布:人工智能学校排名 编辑:程序博客网 时间:2024/06/05 19:56


block会有循环引用的风险--对外部

1。用self时小心

2.借助dealloc方法,判断是否循环引用


- (void)dealloc

{

    NSLog(@"8888-----");

}




解决方法  把self定义成弱引用

__weak typeof(self) weakSelf =self;

    NSBlockOperation *downloadOp = [NSBlockOperationblockOperationWithBlock:^{

    

    NSLog(@"正在下载中......");

    // 1. 下载图片(二进制)

    NSData *data = [NSDatadataWithContentsOfURL:[NSURLURLWithString:app.icon]];

    UIImage *image = [UIImageimageWithData:data];

    

    // 2. 将下载的数据保存到模型

    if (image) {

        [weakSelf.imageCachesetObject:image forKey:app.icon];

        

        // 将图片写入沙盒

        [data writeToFile:[selfcachePathWithUrl:app.icon]atomically:YES];

    }

        // 3. 将操作从操作缓冲池删除

        [weakSelf.operationCacheremoveObjectForKey:app.icon];

        

    // 4. 更新UI

    [[NSOperationQueuemainQueue] addOperationWithBlock:^{

        // 刷新当前行

        [weakSelf.tableViewreloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationNone];

        }];

    }];

0 0
原创粉丝点击