Wait to clear RCTImageView.image until definitively removed from window

Summary: When `RCTImageView` is removed from the view hierarchy, it clears out its `image` to save memory. This makes sense, except that it gets removed from the window (view hierarchy) even when becoming the child of another view.

This fixes the logic so that it only clears out the image if the view hasn't been moved somewhere else within one frame.

@​public

Reviewed By: @javache

Differential Revision: D2493849
This commit is contained in:
Justin Spahr-Summers 2015-09-30 09:37:54 -07:00 committed by facebook-github-bot-2
parent e65cc9d679
commit a4ef7abebb

View File

@ -238,8 +238,22 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
[super didMoveToWindow];
if (!self.window) {
[self clearImage];
} else if (self.src) {
// Don't keep self alive through the asynchronous dispatch, if the intention was to remove the view so it would
// deallocate.
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(self) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
// If we haven't been re-added to a window by this run loop iteration, clear out the image to save memory.
if (!strongSelf.window) {
[strongSelf clearImage];
}
});
} else if (!self.image && self.src) {
[self reloadImage];
}
}