block的使用极大方便了我们的开发,但是不正确的使用block时就会导致意想不到的问题。
文章目录
1. 单层block
dispatch_async(dispatch_get_main_queue(), ^{
[self stopCaptureAfer] ;
});
如上代码block会捕获self这是显而易见的;
2. 嵌套的block
__weak typeof(self) weakSelf = self;
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[self.KVOControllerNonRetaining observe:captureDevice keyPath:@"rampingVideoZoom" options:0 block:^(id _Nullable observer, id _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
__typeof(weakSelf) strongSelf = weakSelf;
strongSelf.rampingVideoZoom = 1;
if (YES) {
dispatch_async(dispatch_get_main_queue(), ^{
[self stopCaptureAfer] ;
});
}
}];
上述代码呢?self的捕获过程又是怎样的呢?
实际的结果是循环引用了;