Avoid self capture in blocks that would extend ImageView instance lifetime.

Summary:
Avoid self capture in blocks that would extend ImageView instance lifetime.

Changelog:
[iOS][Fixed] - Use weakSelf in objc block instead of self.

Reviewed By: RSNara

Differential Revision: D33639088

fbshipit-source-id: b6e5200d9410ddd9e0683ea6d3cc3116591dd31c
This commit is contained in:
Moses DeJong 2022-01-20 15:37:54 -08:00 коммит произвёл Facebook GitHub Bot
Родитель 95585a9730
Коммит f12c8fba60
1 изменённых файлов: 22 добавлений и 14 удалений

Просмотреть файл

@ -313,9 +313,10 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
}
RCTImageLoaderProgressBlock progressHandler = nil;
if (_onProgress) {
if (self.onProgress) {
RCTDirectEventBlock onProgress = self.onProgress;
progressHandler = ^(int64_t loaded, int64_t total) {
self->_onProgress(@{
onProgress(@{
@"loaded": @((double)loaded),
@"total": @((double)total),
});
@ -371,8 +372,9 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
}
if (error) {
__weak RCTImageView *weakSelf = self;
RCTExecuteOnMainQueue(^{
self.image = nil;
weakSelf.image = nil;
});
if (_onError) {
@ -384,33 +386,39 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
return;
}
__weak RCTImageView *weakSelf = self;
void (^setImageBlock)(UIImage *) = ^(UIImage *image) {
RCTImageView *strongSelf = weakSelf;
if (!strongSelf) {
return;
}
if (!isPartialLoad) {
self->_imageSource = source;
self->_pendingImageSource = nil;
strongSelf->_imageSource = source;
strongSelf->_pendingImageSource = nil;
}
self.image = image;
strongSelf.image = image;
if (isPartialLoad) {
if (self->_onPartialLoad) {
self->_onPartialLoad(nil);
if (strongSelf->_onPartialLoad) {
strongSelf->_onPartialLoad(nil);
}
} else {
if (self->_onLoad) {
if (strongSelf->_onLoad) {
RCTImageSource *sourceLoaded = [source imageSourceWithSize:image.size scale:source.scale];
self->_onLoad(onLoadParamsForSource(sourceLoaded));
strongSelf->_onLoad(onLoadParamsForSource(sourceLoaded));
}
if (self->_onLoadEnd) {
self->_onLoadEnd(nil);
if (strongSelf->_onLoadEnd) {
strongSelf->_onLoadEnd(nil);
}
}
};
if (_blurRadius > __FLT_EPSILON__) {
// Blur on a background thread to avoid blocking interaction
CGFloat blurRadius = self.blurRadius;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *blurredImage = RCTBlurredImageWithRadius(loadedImage, self->_blurRadius);
UIImage *blurredImage = RCTBlurredImageWithRadius(loadedImage, blurRadius);
RCTExecuteOnMainQueue(^{
setImageBlock(blurredImage);
});