weakSelf 和 strongSelf

本文探讨了Swift中弱引用(weakSelf)与强引用(strongSelf)的使用场景及原理,通过具体示例解析了如何避免block中产生的循环引用问题。

最近在看SDWebImage源码,碰到一些比较绕的问题,理解了很久,然后在网上查了些的资料,才算是有了一些理解。在此记录一下。

源码如下:


block会copy要在block中使用的实变量,而copy会是变量的retainCount + 1,如若在不注意很容易造成循环引用。而所谓的循环引用的本质就是,两个对象相互引用,从而造成对象不能正常的dealloc。所以解决的办法就是让引用的一方是weak的,这样就使得相互引用的闭环被打破,能够正常的dealloc了。


1)weakSelf的使用:

Apple 官方的建议是,传进 Block 之前,把 ‘self’ 转换成 weak automatic 的变量,这样在 Block 中就不会出现对 self 的强引用。 

上图的代码中,backgroundTaskId是当前这个类的一个属性,在backgroundTaskId初始化的这个方法中,有一个block回调,在这个block的实现中访问需要访问Self,为了避免造成循环引用,此处给当前的Self取了个别名,并用__weak来修饰,目的是告诉编译器,此处是弱引用,不要retain 当前的这个类,也就是所谓的self。

2)为什么会出现strongSelf?

Apple 官方文档有讲到,如果在 Block 执行完成之前,self 被释放了,weakSelf 也会变为 nil。

clang给出的实例代码:

    __weak __typeof__(self) weakSelf = self;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        [weakSelf doSomething];

    });

 clang 的文档表示,在 doSomething 内,weakSelf 不会被释放。但,下面的情况除外:

 

    __weak __typeof__(self) weakSelf = self;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        [weakSelf doSomething];

        [weakSelf doOtherThing];

    });

 

 在 doSomething 中,weakSelf 不会变成 nil,不过在 doSomething 执行完成,调用第二个方法 doOtherThing 的时候,weakSelf 有可能被释放,

 于是,strongSelf 就派上用场了:

    __weak __typeof__(self) weakSelf = self;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        __strong __typeof(self) strongSelf = weakSelf;

        [strongSelf doSomething];

        [strongSelf doOtherThing];

    });

   __strong 确保strongSelf在block中不会被释放。

 

所以就能理解SDWebImage中的那段代码,block在实现的过程中会对wself进行一次强引用,是为了防止在block还未执行完毕,wself在其他线程中被释放,使得wself为nil。

 

 简单的做个小结:

 1、在使用block时,如果block内部需要访问self的方法、属性、或者实例变量应当使用weakSelf

 2、如果在block内需要多次访问self,则需要使用strongSelf

 3、如果在block内部存在多线程环境访问self,则需要使用strongSelf

 4、block本身不存在多线程之分,block执行是否是多线程,取决于当前的持有者是否是以多线程的方式来调用它。




clang的文档链接

https://github.com/CoderBeta/clang-user-manual




处理下面代码可能出现线程问题的情况- (void)shutterCamera { AVCaptureConnection *videoConnection = [self.ImageOutPut connectionWithMediaType:AVMediaTypeVideo]; UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation]; if (!videoConnection || !self.session.isRunning) { ATLog(@"take photo failed!"); return; } AVCaptureVideoOrientation avcaptureOrientation = [self avOrientationForDeviceOrientation:curDeviceOrientation]; [videoConnection setVideoOrientation:avcaptureOrientation]; [videoConnection setVideoScaleAndCropFactor:self.effectiveScale]; MJWeakSelf; [self.ImageOutPut captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ __strong typeof(weakSelf) strongSelf = weakSelf; if (!strongSelf) return; if (imageDataSampleBuffer) { NSData * imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; strongSelf.image = [UIImage imageWithData:imageData]; if (strongSelf.image) { strongSelf.image = [UIImage updateImageSize:strongSelf.image]; } strongSelf.image = [[TZImageManager manager] fixOrientation:strongSelf.image];//[UIImage fixOrientation:weakSelf.image]; PHAuthorizationStatus authorStatus = [PHPhotoLibrary authorizationStatus]; if (authorStatus == PHAuthorizationStatusAuthorized) { [[TZImageManager manager] savePhotoWithImage:strongSelf.image completion:^(PHAsset *asset, NSError *error) { [strongSelf addPHAsset:asset]; }]; }else{ [strongSelf gotoEditImageController:strongSelf.image asset:nil]; } } }); }]; } - (void)gotoEditImageController:(UIImage *)image asset:(PHAsset *)asset { if (!image) { [ATMBPHud showErrorToView:self.view text:@"获取图片失败,请重新再试"]; return; } if (image) { image = [UIImage updateImageSize:image]; } YasicClipPage *vc = [[YasicClipPage alloc] init]; vc.userId = self.userId; vc.attention = self.attention; vc.targetImage = image; vc.searchType = self.searchType; vc.asset = asset; // [self stopSession]; dispatch_async(dispatch_get_main_queue(), ^{ [[KDUserInterfaceTool currenrtopViewController].navigationController pushViewController:vc animated:YES]; [self removeViewController:self]; }); } - (void)addPHAsset:(PHAsset *)asset { MJWeakSelf; TZAssetModel *assetModel = [[TZImageManager manager] createModelWithAsset:asset]; [ATHUD show]; [[TZImageManager manager] requestImageDataForAsset:assetModel.asset completion:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) { [ATHUD dismiss]; UIImage *image = [UIImage imageWithData:imageData];//[[TZImageManager manager] fixOrientation:[UIImage imageWithData:imageData]]; image = [[TZImageManager manager] fixOrientation:image]; [weakSelf gotoEditImageController:image asset:asset]; } progressHandler:^(double progress, NSError *error, BOOL *stop, NSDictionary *info) { ATLog(@"%f",progress); }]; } #pragma --mark 获取大图 - (void)getImageWithModel:(TZAssetModel *)model { if (model == nil) { ATLog(@"图片为空"); return; } [ATHUD show]; WS(weakSelf) [[TZImageManager manager] requestImageDataForAsset:model.asset completion:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) { [ATHUD dismiss]; UIImage *image = [UIImage imageWithData:imageData]; if (weakSelf.downBtnClick) { weakSelf.downBtnClick(image, model); } } progressHandler:^(double progress, NSError *error, BOOL *stop, NSDictionary *info) { ATLog(@"%f",progress); }]; }
最新发布
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值