Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 6d9f12c

Browse files
authored
[ASNetworkImageNode] Assert if both URL and image are set (#2782)
* [ASNetworkImageNode] Assert if both URL and image are set Here's one other idea for addressing this problem. Essentially, we assert if you've set both the URL and the image. I've played around with Pinterest and there's only one case where we hit this (the transition). So I've also added another method (which is a bummer, it's weird I know) but there's one good reason to add this method, ephemeralImage, which is the user doesn't have to manually clear it out like they would if they used defaultImage to save memory. Putting this up for discussion. * Fix comment * Oh yeah, @dynamic, thanks @adlai * Remove ephemeralImage
1 parent 0bda7ee commit 6d9f12c

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

AsyncDisplayKit/ASNetworkImageNode.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,18 @@ NS_ASSUME_NONNULL_BEGIN
5151
/**
5252
* The image to display.
5353
*
54-
* @discussion Setting an image to the image property of an ASNetworkImageNode will cause it to act like a plain
55-
* ASImageNode if a URL is not set as well. As soon as the URL is set the ASNetworkImageNode will act like an
56-
* ASNetworkImageNode and the image property will be managed internally. This means the image property will be cleared
57-
* out and replaced by the placeholder (<defaultImage>) image while loading and the final image after the new image
58-
* data was downloaded and processed. If you want to use a placholder image, use the defaultImage property
59-
* instead.
54+
* @discussion By setting an image to the image property the ASNetworkImageNode will act like a plain ASImageNode.
55+
* As soon as the URL is set the ASNetworkImageNode will act like an ASNetworkImageNode and the image property
56+
* will be managed internally. This means the image property will be cleared out and replaced by the placeholder
57+
* (<defaultImage>) image while loading and the final image after the new image data was downloaded and processed.
58+
* If you want to use a placholder image functionality use the defaultImage property instead.
6059
*/
6160
@property (nullable, nonatomic, strong) UIImage *image;
6261

6362
/**
64-
* A placeholder image to display while the URL is loading.
63+
* A placeholder image to display while the URL is loading. This is slightly different than placeholderImage in the
64+
* ASDisplayNode superclass as defaultImage will *not* be displayed synchronously. If you wish to have the image
65+
* displayed synchronously, use @c placeholderImage.
6566
*/
6667
@property (nullable, nonatomic, strong, readwrite) UIImage *defaultImage;
6768

AsyncDisplayKit/ASNetworkImageNode.mm

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ - (void)setImage:(UIImage *)image
114114
{
115115
ASDN::MutexLocker l(__instanceLock__);
116116

117-
_imageWasSetExternally = (image != nil && _URL == nil);
117+
_imageWasSetExternally = (image != nil);
118+
if (_imageWasSetExternally) {
119+
ASDisplayNodeAssertNil(_URL, @"Directly setting an image on an ASNetworkImageNode causes it to behave like an ASImageNode instead of an ASNetworkImageNode. If this is what you want, set the URL to nil first.");
120+
[self _cancelDownloadAndClearImage];
121+
_URL = nil;
122+
}
118123

119124
[self _setImage:image];
120125
}
@@ -134,6 +139,8 @@ - (void)setURL:(NSURL *)URL resetToDefault:(BOOL)reset
134139
{
135140
ASDN::MutexLocker l(__instanceLock__);
136141

142+
ASDisplayNodeAssert(_imageWasSetExternally == NO, @"Setting a URL to an ASNetworkImageNode after setting an image changes its behavior from an ASImageNode to an ASNetworkImageNode. If this is what you want, set the image to nil first.");
143+
137144
_imageWasSetExternally = NO;
138145

139146
if (ASObjectIsEqual(URL, _URL)) {

0 commit comments

Comments
 (0)