From 929d118c962dcf595386daf5c91ce30e1f8e577d Mon Sep 17 00:00:00 2001 From: Max Wang Date: Sun, 16 Jul 2017 16:59:28 -0700 Subject: [PATCH 01/16] fix SIMULATE_WEB_RESPONSE not imported #449 --- examples/ASCollectionView/Sample/ViewController.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/ASCollectionView/Sample/ViewController.m b/examples/ASCollectionView/Sample/ViewController.m index d00ea60df..4e6a04e97 100644 --- a/examples/ASCollectionView/Sample/ViewController.m +++ b/examples/ASCollectionView/Sample/ViewController.m @@ -16,7 +16,7 @@ // #import "ViewController.h" - +#import "AppDelegate.h" #import #import "SupplementaryNode.h" #import "ItemNode.h" @@ -70,8 +70,8 @@ - (void)viewDidLoad { NSLog(@"ViewController is not nil"); strongSelf->_data = [[NSArray alloc] init]; - [strongSelf->_collectionView performBatchUpdates:^{ - [strongSelf->_collectionView insertSections:[[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, 100)]]; + [strongSelf->_collectionNode performBatchUpdates:^{ + [strongSelf->_collectionNode insertSections:[[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, 100)]]; } completion:nil]; NSLog(@"ViewController finished updating collectionView"); } @@ -81,7 +81,7 @@ - (void)viewDidLoad }; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), mockWebService); - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.navigationController popViewControllerAnimated:YES]; }); #endif From 329f35ff56aa6605ba9157174568cfd18a7a5373 Mon Sep 17 00:00:00 2001 From: Max Wang Date: Mon, 5 Feb 2018 14:10:21 -0800 Subject: [PATCH 02/16] Fix to make rangeMode update in right time --- Source/ASCollectionView.mm | 1 + Source/ASTableView.mm | 1 + Source/Details/ASRangeController.h | 5 +++++ Source/Details/ASRangeController.mm | 14 ++++++++++---- Tests/ASCollectionViewTests.mm | 1 + 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Source/ASCollectionView.mm b/Source/ASCollectionView.mm index 0c706faa4..7c20dbddf 100644 --- a/Source/ASCollectionView.mm +++ b/Source/ASCollectionView.mm @@ -1511,6 +1511,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView // If a scroll happenes the current range mode needs to go to full ASInterfaceState interfaceState = [self interfaceStateForRangeController:_rangeController]; if (ASInterfaceStateIncludesVisible(interfaceState)) { + _rangeController.contentOffsetHasChanged = YES; [_rangeController updateCurrentRangeWithMode:ASLayoutRangeModeFull]; [self _checkForBatchFetching]; } diff --git a/Source/ASTableView.mm b/Source/ASTableView.mm index 4606726f2..7bcd0fd5c 100644 --- a/Source/ASTableView.mm +++ b/Source/ASTableView.mm @@ -1218,6 +1218,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView // If a scroll happenes the current range mode needs to go to full ASInterfaceState interfaceState = [self interfaceStateForRangeController:_rangeController]; if (ASInterfaceStateIncludesVisible(interfaceState)) { + _rangeController.contentOffsetHasChanged = YES; [_rangeController updateCurrentRangeWithMode:ASLayoutRangeModeFull]; [self _checkForBatchFetching]; } diff --git a/Source/Details/ASRangeController.h b/Source/Details/ASRangeController.h index 46a5cbf69..152c4e8b5 100644 --- a/Source/Details/ASRangeController.h +++ b/Source/Details/ASRangeController.h @@ -100,6 +100,11 @@ AS_SUBCLASSING_RESTRICTED */ @property (nonatomic, weak) id delegate; +/** + * Property that indicates whether the scroll view for this range controller has ever changed its contentOffset. + */ +@property (nonatomic, assign) BOOL contentOffsetHasChanged; + @end diff --git a/Source/Details/ASRangeController.mm b/Source/Details/ASRangeController.mm index 818fb3a1d..4d10838b7 100644 --- a/Source/Details/ASRangeController.mm +++ b/Source/Details/ASRangeController.mm @@ -45,6 +45,7 @@ @interface ASRangeController () NSSet *_allPreviousIndexPaths; NSHashTable *_visibleNodes; ASLayoutRangeMode _currentRangeMode; + BOOL _contentOffsetHasChanged; BOOL _preserveCurrentRangeMode; BOOL _didRegisterForNodeDisplayNotifications; CFTimeInterval _pendingDisplayNodesTimestamp; @@ -77,6 +78,7 @@ - (instancetype)init _rangeIsValid = YES; _currentRangeMode = ASLayoutRangeModeUnspecified; + _contentOffsetHasChanged = NO; _preserveCurrentRangeMode = NO; _previousScrollDirection = ASScrollDirectionDown | ASScrollDirectionRight; @@ -237,9 +239,13 @@ - (void)_updateVisibleNodeIndexPaths ASInterfaceState selfInterfaceState = [self interfaceState]; ASLayoutRangeMode rangeMode = _currentRangeMode; - // If the range mode is explicitly set via updateCurrentRangeWithMode: it will last in that mode until the - // range controller becomes visible again or explicitly changes the range mode again - if ((!_preserveCurrentRangeMode && ASInterfaceStateIncludesVisible(selfInterfaceState)) || [[self class] isFirstRangeUpdateForRangeMode:rangeMode]) { + BOOL updateRangeMode = !_preserveCurrentRangeMode && _contentOffsetHasChanged; + + // If we've never scrolled before, we never update the range mode, so it doesn't jump into Full too early. + // This can happen if we have multiple, noisy updates occurring from application code before the user has engaged. + // If the range mode is explicitly set via updateCurrentRangeWithMode:, we'll preserve that for at least one update cycle. + // Once the user has scrolled and the range is visible, we'll always resume managing the range mode automatically. + if ((updateRangeMode && ASInterfaceStateIncludesVisible(selfInterfaceState)) || [[self class] isFirstRangeUpdateForRangeMode:rangeMode]) { rangeMode = [ASRangeController rangeModeForInterfaceState:selfInterfaceState currentRangeMode:_currentRangeMode]; } @@ -412,7 +418,7 @@ - (void)_updateVisibleNodeIndexPaths // NSLog(@"custom: %@", visibleNodePathsSet); // } [modifiedIndexPaths sortUsingSelector:@selector(compare:)]; - NSLog(@"Range update complete; modifiedIndexPaths: %@", [self descriptionWithIndexPaths:modifiedIndexPaths]); + NSLog(@"Range update complete; modifiedIndexPaths: %@, rangeMode: %d", [self descriptionWithIndexPaths:modifiedIndexPaths], rangeMode); #endif ASSignpostEnd(ASSignpostRangeControllerUpdate); diff --git a/Tests/ASCollectionViewTests.mm b/Tests/ASCollectionViewTests.mm index b223e246f..11ab711d6 100644 --- a/Tests/ASCollectionViewTests.mm +++ b/Tests/ASCollectionViewTests.mm @@ -1071,6 +1071,7 @@ - (void)testInitialRangeBounds for (NSInteger i = 0; i < c; i++) { NSIndexPath *ip = [NSIndexPath indexPathForItem:i inSection:s]; ASCellNode *node = [cn nodeForItemAtIndexPath:ip]; + [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.001]]; if (node.inPreloadState) { CGRect frame = [cn.view layoutAttributesForItemAtIndexPath:ip].frame; r = CGRectUnion(r, frame); From 5fabc1e0a9ff09adb272a31c364a84b4fb1113fd Mon Sep 17 00:00:00 2001 From: Max Wang Date: Wed, 16 May 2018 22:20:34 -0700 Subject: [PATCH 03/16] remove uncessary assert --- Source/Details/ASDataController.mm | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Source/Details/ASDataController.mm b/Source/Details/ASDataController.mm index f48c8dfc7..71282be8c 100644 --- a/Source/Details/ASDataController.mm +++ b/Source/Details/ASDataController.mm @@ -452,14 +452,6 @@ - (void)waitUntilAllUpdatesAreProcessed - (BOOL)isProcessingUpdates { ASDisplayNodeAssertMainThread(); -#if ASDISPLAYNODE_ASSERTIONS_ENABLED - // Using dispatch_group_wait is much more expensive than our manually managed count, but it's crucial they always match. - BOOL editingTransactionQueueBusy = dispatch_group_wait(_editingTransactionGroup, DISPATCH_TIME_NOW) != 0; - ASDisplayNodeAssert(editingTransactionQueueBusy == (_editingTransactionGroupCount > 0), - @"editingTransactionQueueBusy = %@, but _editingTransactionGroupCount = %d !", - editingTransactionQueueBusy ? @"YES" : @"NO", (int)_editingTransactionGroupCount); -#endif - return _mainSerialQueue.numberOfScheduledBlocks > 0 || _editingTransactionGroupCount > 0; } From 324d3b0adb4f7f5cae432cbd31aa395e9f77bd61 Mon Sep 17 00:00:00 2001 From: Max Wang Date: Tue, 3 Jul 2018 17:11:59 -0700 Subject: [PATCH 04/16] add api to allow delegated calls in background. --- Source/ASNetworkImageNode.h | 44 ++++++++++++++++++--- Source/ASNetworkImageNode.mm | 75 ++++++++++++++++++++++++++++++++++-- 2 files changed, 109 insertions(+), 10 deletions(-) diff --git a/Source/ASNetworkImageNode.h b/Source/ASNetworkImageNode.h index 494925b89..7dfd812dc 100644 --- a/Source/ASNetworkImageNode.h +++ b/Source/ASNetworkImageNode.h @@ -156,9 +156,9 @@ NS_ASSUME_NONNULL_BEGIN * @param image The newly-loaded image. * @param info Additional information about the image load. * - * @discussion Called on a background queue. + * @discussion Called on a main queue. */ -- (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image info:(ASNetworkImageLoadInfo *)info; +- (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image info:(ASNetworkImageLoadInfo *)info ASDISPLAYNODE_DEPRECATED_MSG("Please use didFetchImage:info: instead.");; /** * Notification that the image node finished downloading an image. @@ -166,9 +166,9 @@ NS_ASSUME_NONNULL_BEGIN * @param imageNode The sender. * @param image The newly-loaded image. * - * @discussion Called on a background queue. + * @discussion Called on a main queue. */ -- (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image; +- (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image ASDISPLAYNODE_DEPRECATED_MSG("Please use didFetchImage: instead."); /** * Notification that the image node started to load @@ -185,9 +185,9 @@ NS_ASSUME_NONNULL_BEGIN * @param imageNode The sender. * @param error The error with details. * - * @discussion Called on a background queue. + * @discussion Called on a main queue. */ -- (void)imageNode:(ASNetworkImageNode *)imageNode didFailWithError:(NSError *)error; +- (void)imageNode:(ASNetworkImageNode *)imageNode didFailWithError:(NSError *)error ASDISPLAYNODE_DEPRECATED_MSG("Please use didFailFetchingWithError: instead."); /** * Notification that the image node finished decoding an image. @@ -196,6 +196,38 @@ NS_ASSUME_NONNULL_BEGIN */ - (void)imageNodeDidFinishDecoding:(ASNetworkImageNode *)imageNode; +/** + * Notification that the image node finished downloading an image, with additional info. + * If implemented, this method will be called instead of `imageNode:didFetchImage:`. + * + * @param imageNode The sender. + * @param image The newly-loaded image. + * @param info Additional information about the image load. + * + * @discussion Called on a background queue. + */ +- (void)imageNode:(ASNetworkImageNode *)imageNode didFetchImage:(UIImage *)image info:(ASNetworkImageLoadInfo *)info; + +/** + * Notification that the image node finished downloading an image. + * + * @param imageNode The sender. + * @param image The newly-loaded image. + * + * @discussion Called on a background queue. + */ +- (void)imageNode:(ASNetworkImageNode *)imageNode didFetchImage:(UIImage *)image; + +/** + * Notification that the image node failed to download the image. + * + * @param imageNode The sender. + * @param error The error with details. + * + * @discussion Called on a background queue. + */ +- (void)imageNode:(ASNetworkImageNode *)imageNode didFailFetchingWithError:(NSError *)error; + @end NS_ASSUME_NONNULL_END diff --git a/Source/ASNetworkImageNode.mm b/Source/ASNetworkImageNode.mm index f28e063ff..b44996464 100755 --- a/Source/ASNetworkImageNode.mm +++ b/Source/ASNetworkImageNode.mm @@ -61,6 +61,9 @@ @interface ASNetworkImageNode () unsigned int delegateDidFinishDecoding:1; unsigned int delegateDidLoadImage:1; unsigned int delegateDidLoadImageWithInfo:1; + unsigned int delegateDidFailFetchingWithError:1; + unsigned int delegateDidFetchImage:1; + unsigned int delegateDidFetchImageWithInfo:1; } _delegateFlags; @@ -304,6 +307,9 @@ - (void)setDelegate:(id)delegate _delegateFlags.delegateDidFinishDecoding = [delegate respondsToSelector:@selector(imageNodeDidFinishDecoding:)]; _delegateFlags.delegateDidLoadImage = [delegate respondsToSelector:@selector(imageNode:didLoadImage:)]; _delegateFlags.delegateDidLoadImageWithInfo = [delegate respondsToSelector:@selector(imageNode:didLoadImage:info:)]; + _delegateFlags.delegateDidFailFetchingWithError = [delegate respondsToSelector:@selector(imageNode:didFailFetchingWithError:)]; + _delegateFlags.delegateDidFetchImageWithInfo = [delegate respondsToSelector:@selector(imageNode:didFetchImage:info:)]; + _delegateFlags.delegateDidFetchImage = [delegate respondsToSelector:@selector(imageNode:didFetchImage:)]; } - (id)delegate @@ -352,10 +358,26 @@ - (void)displayWillStartAsynchronously:(BOOL)asynchronously if (_delegateFlags.delegateDidLoadImageWithInfo) { ASUnlockScope(self); auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:url sourceType:ASNetworkImageSourceSynchronousCache downloadIdentifier:nil userInfo:nil]; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" [_delegate imageNode:self didLoadImage:result info:info]; +#pragma clang diagnostic pop } else if (_delegateFlags.delegateDidLoadImage) { ASUnlockScope(self); +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" [_delegate imageNode:self didLoadImage:result]; +#pragma clang diagnostic pop + } + + // Call out to the delegate. + if (_delegateFlags.delegateDidFetchImageWithInfo) { + ASUnlockScope(self); + auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:url sourceType:ASNetworkImageSourceSynchronousCache downloadIdentifier:nil userInfo:nil]; + [_delegate imageNode:self didFetchImage:result info:info]; + } else if (_delegateFlags.delegateDidFetchImage) { + ASUnlockScope(self); + [_delegate imageNode:self didFetchImage:result]; } } } @@ -666,10 +688,26 @@ - (void)_lazilyLoadImageIfNecessary if (_delegateFlags.delegateDidLoadImageWithInfo) { ASUnlockScope(self); auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:ASNetworkImageSourceFileURL downloadIdentifier:nil userInfo:nil]; - [delegate imageNode:self didLoadImage:self.image info:info]; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + [_delegate imageNode:self didLoadImage:self.image info:info]; +#pragma clang diagnostic pop } else if (_delegateFlags.delegateDidLoadImage) { ASUnlockScope(self); - [delegate imageNode:self didLoadImage:self.image]; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + [_delegate imageNode:self didLoadImage:self.image]; +#pragma clang diagnostic pop + } + + // Call out to the delegate. + if (_delegateFlags.delegateDidFetchImageWithInfo) { + ASUnlockScope(self); + auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:ASNetworkImageSourceFileURL downloadIdentifier:nil userInfo:nil]; + [_delegate imageNode:self didFetchImage:self.image info:info]; + } else if (_delegateFlags.delegateDidFetchImage) { + ASUnlockScope(self); + [_delegate imageNode:self didFetchImage:self.image]; } }); } else { @@ -716,24 +754,53 @@ - (void)_lazilyLoadImageIfNecessary strongSelf->_cacheSentinel++; void (^calloutBlock)(ASNetworkImageNode *inst); - + void (^backgroundCalloutBlock)(ASNetworkImageNode *inst); + if (newImage) { if (_delegateFlags.delegateDidLoadImageWithInfo) { calloutBlock = ^(ASNetworkImageNode *strongSelf) { auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:imageSource downloadIdentifier:downloadIdentifier userInfo:userInfo]; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" [delegate imageNode:strongSelf didLoadImage:newImage info:info]; +#pragma clang diagnostic pop }; } else if (_delegateFlags.delegateDidLoadImage) { calloutBlock = ^(ASNetworkImageNode *strongSelf) { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" [delegate imageNode:strongSelf didLoadImage:newImage]; +#pragma clang diagnostic pop }; } } else if (error && _delegateFlags.delegateDidFailWithError) { calloutBlock = ^(ASNetworkImageNode *strongSelf) { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" [delegate imageNode:strongSelf didFailWithError:error]; +#pragma clang diagnostic pop }; } - + + if (_delegateFlags.delegateDidLoadImageWithInfo) { + backgroundCalloutBlock = ^(ASNetworkImageNode *strongSelf) { + auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:imageSource downloadIdentifier:downloadIdentifier userInfo:userInfo]; + [delegate imageNode:strongSelf didFetchImage:newImage info:info]; + }; + } else if (_delegateFlags.delegateDidFetchImage) { + backgroundCalloutBlock = ^(ASNetworkImageNode *strongSelf) { + [delegate imageNode:strongSelf didFetchImage:newImage]; + }; + } else if (error && _delegateFlags.delegateDidFailFetchingWithError) { + backgroundCalloutBlock = ^(ASNetworkImageNode *strongSelf) { + [delegate imageNode:strongSelf didFailFetchingWithError:error]; + }; + } + + if (backgroundCalloutBlock) { + backgroundCalloutBlock(self); + } + if (calloutBlock) { ASPerformBlockOnMainThread(^{ if (auto strongSelf = weakSelf) { From 018867e7ff30647b3ffb44588084a24f75a70d9f Mon Sep 17 00:00:00 2001 From: Max Wang Date: Tue, 3 Jul 2018 17:20:16 -0700 Subject: [PATCH 05/16] fix typo --- Source/ASNetworkImageNode.mm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/ASNetworkImageNode.mm b/Source/ASNetworkImageNode.mm index b44996464..7e357d296 100755 --- a/Source/ASNetworkImageNode.mm +++ b/Source/ASNetworkImageNode.mm @@ -690,13 +690,13 @@ - (void)_lazilyLoadImageIfNecessary auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:ASNetworkImageSourceFileURL downloadIdentifier:nil userInfo:nil]; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [_delegate imageNode:self didLoadImage:self.image info:info]; + [delegate imageNode:self didLoadImage:self.image info:info]; #pragma clang diagnostic pop } else if (_delegateFlags.delegateDidLoadImage) { ASUnlockScope(self); #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [_delegate imageNode:self didLoadImage:self.image]; + [delegate imageNode:self didLoadImage:self.image]; #pragma clang diagnostic pop } @@ -782,7 +782,7 @@ - (void)_lazilyLoadImageIfNecessary }; } - if (_delegateFlags.delegateDidLoadImageWithInfo) { + if (_delegateFlags.delegateDidFetchImageWithInfo) { backgroundCalloutBlock = ^(ASNetworkImageNode *strongSelf) { auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:imageSource downloadIdentifier:downloadIdentifier userInfo:userInfo]; [delegate imageNode:strongSelf didFetchImage:newImage info:info]; From 1da91a68f3d4dd567be3a7750d416ef0c3d8c050 Mon Sep 17 00:00:00 2001 From: Max Wang Date: Thu, 12 Jul 2018 10:07:51 -0700 Subject: [PATCH 06/16] 1. Add class property to decide whether to send delegate callbacks on main or background. 2. remove non-info api. --- Source/ASNetworkImageNode.h | 19 ++---- Source/ASNetworkImageNode.mm | 124 ++++++++++++++++------------------- 2 files changed, 65 insertions(+), 78 deletions(-) diff --git a/Source/ASNetworkImageNode.h b/Source/ASNetworkImageNode.h index 7dfd812dc..7262562af 100644 --- a/Source/ASNetworkImageNode.h +++ b/Source/ASNetworkImageNode.h @@ -56,6 +56,11 @@ NS_ASSUME_NONNULL_BEGIN */ @property (nullable, weak) id delegate; +/** + * The delegate will receive callbacks on main thread. Default to YES. + */ +@property (class) BOOL delegateCallbacksOnMainThread; + /** * The image to display. * @@ -166,7 +171,7 @@ NS_ASSUME_NONNULL_BEGIN * @param imageNode The sender. * @param image The newly-loaded image. * - * @discussion Called on a main queue. + * @discussion Called on a main thread. */ - (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image ASDISPLAYNODE_DEPRECATED_MSG("Please use didFetchImage: instead."); @@ -185,7 +190,7 @@ NS_ASSUME_NONNULL_BEGIN * @param imageNode The sender. * @param error The error with details. * - * @discussion Called on a main queue. + * @discussion Called on a main thread. */ - (void)imageNode:(ASNetworkImageNode *)imageNode didFailWithError:(NSError *)error ASDISPLAYNODE_DEPRECATED_MSG("Please use didFailFetchingWithError: instead."); @@ -208,16 +213,6 @@ NS_ASSUME_NONNULL_BEGIN */ - (void)imageNode:(ASNetworkImageNode *)imageNode didFetchImage:(UIImage *)image info:(ASNetworkImageLoadInfo *)info; -/** - * Notification that the image node finished downloading an image. - * - * @param imageNode The sender. - * @param image The newly-loaded image. - * - * @discussion Called on a background queue. - */ -- (void)imageNode:(ASNetworkImageNode *)imageNode didFetchImage:(UIImage *)image; - /** * Notification that the image node failed to download the image. * diff --git a/Source/ASNetworkImageNode.mm b/Source/ASNetworkImageNode.mm index 7e357d296..269f987e0 100755 --- a/Source/ASNetworkImageNode.mm +++ b/Source/ASNetworkImageNode.mm @@ -62,7 +62,6 @@ @interface ASNetworkImageNode () unsigned int delegateDidLoadImage:1; unsigned int delegateDidLoadImageWithInfo:1; unsigned int delegateDidFailFetchingWithError:1; - unsigned int delegateDidFetchImage:1; unsigned int delegateDidFetchImageWithInfo:1; } _delegateFlags; @@ -110,6 +109,8 @@ - (instancetype)initWithCache:(id)cache downloader:(id)delegate _delegateFlags.delegateDidLoadImageWithInfo = [delegate respondsToSelector:@selector(imageNode:didLoadImage:info:)]; _delegateFlags.delegateDidFailFetchingWithError = [delegate respondsToSelector:@selector(imageNode:didFailFetchingWithError:)]; _delegateFlags.delegateDidFetchImageWithInfo = [delegate respondsToSelector:@selector(imageNode:didFetchImage:info:)]; - _delegateFlags.delegateDidFetchImage = [delegate respondsToSelector:@selector(imageNode:didFetchImage:)]; } - (id)delegate @@ -355,7 +355,7 @@ - (void)displayWillStartAsynchronously:(BOOL)asynchronously _imageLoaded = YES; // Call out to the delegate. - if (_delegateFlags.delegateDidLoadImageWithInfo) { + if (ASNetworkImageNode.delegateCallbacksOnMainThread && _delegateFlags.delegateDidLoadImageWithInfo) { ASUnlockScope(self); auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:url sourceType:ASNetworkImageSourceSynchronousCache downloadIdentifier:nil userInfo:nil]; #pragma clang diagnostic push @@ -371,13 +371,10 @@ - (void)displayWillStartAsynchronously:(BOOL)asynchronously } // Call out to the delegate. - if (_delegateFlags.delegateDidFetchImageWithInfo) { + if (!ASNetworkImageNode.delegateCallbacksOnMainThread && _delegateFlags.delegateDidFetchImageWithInfo) { ASUnlockScope(self); auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:url sourceType:ASNetworkImageSourceSynchronousCache downloadIdentifier:nil userInfo:nil]; [_delegate imageNode:self didFetchImage:result info:info]; - } else if (_delegateFlags.delegateDidFetchImage) { - ASUnlockScope(self); - [_delegate imageNode:self didFetchImage:result]; } } } @@ -684,30 +681,28 @@ - (void)_lazilyLoadImageIfNecessary _imageLoaded = YES; [self _setCurrentImageQuality:1.0]; - - if (_delegateFlags.delegateDidLoadImageWithInfo) { - ASUnlockScope(self); - auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:ASNetworkImageSourceFileURL downloadIdentifier:nil userInfo:nil]; -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - [delegate imageNode:self didLoadImage:self.image info:info]; -#pragma clang diagnostic pop - } else if (_delegateFlags.delegateDidLoadImage) { - ASUnlockScope(self); -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - [delegate imageNode:self didLoadImage:self.image]; -#pragma clang diagnostic pop - } - - // Call out to the delegate. - if (_delegateFlags.delegateDidFetchImageWithInfo) { - ASUnlockScope(self); - auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:ASNetworkImageSourceFileURL downloadIdentifier:nil userInfo:nil]; - [_delegate imageNode:self didFetchImage:self.image info:info]; - } else if (_delegateFlags.delegateDidFetchImage) { - ASUnlockScope(self); - [_delegate imageNode:self didFetchImage:self.image]; + if (ASNetworkImageNode.delegateCallbacksOnMainThread) { + if (_delegateFlags.delegateDidLoadImageWithInfo) { + ASUnlockScope(self); + auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:ASNetworkImageSourceFileURL downloadIdentifier:nil userInfo:nil]; + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wdeprecated-declarations" + [delegate imageNode:self didLoadImage:self.image info:info]; + #pragma clang diagnostic pop + } else if (_delegateFlags.delegateDidLoadImage) { + ASUnlockScope(self); + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wdeprecated-declarations" + [delegate imageNode:self didLoadImage:self.image]; + #pragma clang diagnostic pop + } + } else { + // Call out to the delegate. + if (_delegateFlags.delegateDidFetchImageWithInfo) { + ASUnlockScope(self); + auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:ASNetworkImageSourceFileURL downloadIdentifier:nil userInfo:nil]; + [_delegate imageNode:self didFetchImage:self.image info:info]; + } } }); } else { @@ -755,46 +750,43 @@ - (void)_lazilyLoadImageIfNecessary void (^calloutBlock)(ASNetworkImageNode *inst); void (^backgroundCalloutBlock)(ASNetworkImageNode *inst); - - if (newImage) { - if (_delegateFlags.delegateDidLoadImageWithInfo) { + if (ASNetworkImageNode.delegateCallbacksOnMainThread) { + if (newImage) { + if (_delegateFlags.delegateDidLoadImageWithInfo) { + calloutBlock = ^(ASNetworkImageNode *strongSelf) { + auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:imageSource downloadIdentifier:downloadIdentifier userInfo:userInfo]; + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wdeprecated-declarations" + [delegate imageNode:strongSelf didLoadImage:newImage info:info]; + #pragma clang diagnostic pop + }; + } else if (_delegateFlags.delegateDidLoadImage) { + calloutBlock = ^(ASNetworkImageNode *strongSelf) { + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wdeprecated-declarations" + [delegate imageNode:strongSelf didLoadImage:newImage]; + #pragma clang diagnostic pop + }; + } + } else if (error && _delegateFlags.delegateDidFailWithError) { calloutBlock = ^(ASNetworkImageNode *strongSelf) { + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wdeprecated-declarations" + [delegate imageNode:strongSelf didFailWithError:error]; + #pragma clang diagnostic pop + }; + } + } else { // !ASNetworkImageNode.delegateCallbacksOnMainThread + if (newImage && _delegateFlags.delegateDidFetchImageWithInfo) { + backgroundCalloutBlock = ^(ASNetworkImageNode *strongSelf) { auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:imageSource downloadIdentifier:downloadIdentifier userInfo:userInfo]; -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - [delegate imageNode:strongSelf didLoadImage:newImage info:info]; -#pragma clang diagnostic pop + [delegate imageNode:strongSelf didFetchImage:newImage info:info]; }; - } else if (_delegateFlags.delegateDidLoadImage) { - calloutBlock = ^(ASNetworkImageNode *strongSelf) { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - [delegate imageNode:strongSelf didLoadImage:newImage]; -#pragma clang diagnostic pop + } else if (error && _delegateFlags.delegateDidFailFetchingWithError) { + backgroundCalloutBlock = ^(ASNetworkImageNode *strongSelf) { + [delegate imageNode:strongSelf didFailFetchingWithError:error]; }; } - } else if (error && _delegateFlags.delegateDidFailWithError) { - calloutBlock = ^(ASNetworkImageNode *strongSelf) { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - [delegate imageNode:strongSelf didFailWithError:error]; -#pragma clang diagnostic pop - }; - } - - if (_delegateFlags.delegateDidFetchImageWithInfo) { - backgroundCalloutBlock = ^(ASNetworkImageNode *strongSelf) { - auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:imageSource downloadIdentifier:downloadIdentifier userInfo:userInfo]; - [delegate imageNode:strongSelf didFetchImage:newImage info:info]; - }; - } else if (_delegateFlags.delegateDidFetchImage) { - backgroundCalloutBlock = ^(ASNetworkImageNode *strongSelf) { - [delegate imageNode:strongSelf didFetchImage:newImage]; - }; - } else if (error && _delegateFlags.delegateDidFailFetchingWithError) { - backgroundCalloutBlock = ^(ASNetworkImageNode *strongSelf) { - [delegate imageNode:strongSelf didFailFetchingWithError:error]; - }; } if (backgroundCalloutBlock) { From bdbd7de60ba44117e482470a8cffbb2e663ae7b9 Mon Sep 17 00:00:00 2001 From: Max Wang Date: Thu, 12 Jul 2018 10:30:09 -0700 Subject: [PATCH 07/16] Refactor. --- Source/ASNetworkImageNode.h | 4 ++-- Source/ASNetworkImageNode.mm | 29 ++++++++++++++++------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Source/ASNetworkImageNode.h b/Source/ASNetworkImageNode.h index 7262562af..a2115020d 100644 --- a/Source/ASNetworkImageNode.h +++ b/Source/ASNetworkImageNode.h @@ -163,7 +163,7 @@ NS_ASSUME_NONNULL_BEGIN * * @discussion Called on a main queue. */ -- (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image info:(ASNetworkImageLoadInfo *)info ASDISPLAYNODE_DEPRECATED_MSG("Please use didFetchImage:info: instead.");; +- (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image info:(ASNetworkImageLoadInfo *)info ASDISPLAYNODE_DEPRECATED_MSG("Please use didFetchImage:info: instead."); /** * Notification that the image node finished downloading an image. @@ -173,7 +173,7 @@ NS_ASSUME_NONNULL_BEGIN * * @discussion Called on a main thread. */ -- (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image ASDISPLAYNODE_DEPRECATED_MSG("Please use didFetchImage: instead."); +- (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image ASDISPLAYNODE_DEPRECATED_MSG("Please use didFetchImage:info: instead."); /** * Notification that the image node started to load diff --git a/Source/ASNetworkImageNode.mm b/Source/ASNetworkImageNode.mm index 269f987e0..cedb066ae 100755 --- a/Source/ASNetworkImageNode.mm +++ b/Source/ASNetworkImageNode.mm @@ -109,7 +109,10 @@ - (instancetype)initWithCache:(id)cache downloader:(id_cacheSentinel++; void (^calloutBlock)(ASNetworkImageNode *inst); - void (^backgroundCalloutBlock)(ASNetworkImageNode *inst); + if (ASNetworkImageNode.delegateCallbacksOnMainThread) { if (newImage) { if (_delegateFlags.delegateDidLoadImageWithInfo) { @@ -778,27 +781,27 @@ - (void)_lazilyLoadImageIfNecessary } } else { // !ASNetworkImageNode.delegateCallbacksOnMainThread if (newImage && _delegateFlags.delegateDidFetchImageWithInfo) { - backgroundCalloutBlock = ^(ASNetworkImageNode *strongSelf) { + calloutBlock = ^(ASNetworkImageNode *strongSelf) { auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:imageSource downloadIdentifier:downloadIdentifier userInfo:userInfo]; [delegate imageNode:strongSelf didFetchImage:newImage info:info]; }; } else if (error && _delegateFlags.delegateDidFailFetchingWithError) { - backgroundCalloutBlock = ^(ASNetworkImageNode *strongSelf) { + calloutBlock = ^(ASNetworkImageNode *strongSelf) { [delegate imageNode:strongSelf didFailFetchingWithError:error]; }; } } - if (backgroundCalloutBlock) { - backgroundCalloutBlock(self); - } - if (calloutBlock) { - ASPerformBlockOnMainThread(^{ - if (auto strongSelf = weakSelf) { - calloutBlock(strongSelf); - } - }); + if (ASNetworkImageNode.delegateCallbacksOnMainThread) { + ASPerformBlockOnMainThread(^{ + if (auto strongSelf = weakSelf) { + calloutBlock(strongSelf); + } + }); + } else { + calloutBlock(self); + } } }); }; From 8480a75848dcb40398363866c0a3dd698cedf93f Mon Sep 17 00:00:00 2001 From: Max Wang Date: Thu, 12 Jul 2018 11:43:34 -0700 Subject: [PATCH 08/16] add ivar for class property. --- Source/ASNetworkImageNode.mm | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Source/ASNetworkImageNode.mm b/Source/ASNetworkImageNode.mm index cedb066ae..c5ce42638 100755 --- a/Source/ASNetworkImageNode.mm +++ b/Source/ASNetworkImageNode.mm @@ -87,6 +87,8 @@ @interface ASNetworkImageNode () @implementation ASNetworkImageNode +static BOOL _delegateCallbacksOnMainThread = YES; + @dynamic image; - (instancetype)initWithCache:(id)cache downloader:(id)downloader @@ -448,6 +450,14 @@ - (void)didEnterPreloadState [self _lazilyLoadImageIfNecessary]; } ++ (void)setDelegateCallbacksOnMainThread:(BOOL)delegateCallbacksOnMainThread { + _delegateCallbacksOnMainThread = delegateCallbacksOnMainThread; +} + ++ (BOOL)delegateCallbacksOnMainThread { + return _delegateCallbacksOnMainThread; +} + #pragma mark - Progress - (void)handleProgressImage:(UIImage *)progressImage progress:(CGFloat)progress downloadIdentifier:(nullable id)downloadIdentifier From c17c0a11f36b5ab6321cca6902499127881131f5 Mon Sep 17 00:00:00 2001 From: Max Wang Date: Fri, 13 Jul 2018 10:55:18 -0700 Subject: [PATCH 09/16] Donot use extra api. --- Source/ASNetworkImageNode.h | 28 ++--------- Source/ASNetworkImageNode.mm | 91 +++++++----------------------------- 2 files changed, 20 insertions(+), 99 deletions(-) diff --git a/Source/ASNetworkImageNode.h b/Source/ASNetworkImageNode.h index a2115020d..95a7cabc7 100644 --- a/Source/ASNetworkImageNode.h +++ b/Source/ASNetworkImageNode.h @@ -163,7 +163,7 @@ NS_ASSUME_NONNULL_BEGIN * * @discussion Called on a main queue. */ -- (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image info:(ASNetworkImageLoadInfo *)info ASDISPLAYNODE_DEPRECATED_MSG("Please use didFetchImage:info: instead."); +- (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image info:(ASNetworkImageLoadInfo *)info; /** * Notification that the image node finished downloading an image. @@ -173,7 +173,7 @@ NS_ASSUME_NONNULL_BEGIN * * @discussion Called on a main thread. */ -- (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image ASDISPLAYNODE_DEPRECATED_MSG("Please use didFetchImage:info: instead."); +- (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image; /** * Notification that the image node started to load @@ -192,7 +192,7 @@ NS_ASSUME_NONNULL_BEGIN * * @discussion Called on a main thread. */ -- (void)imageNode:(ASNetworkImageNode *)imageNode didFailWithError:(NSError *)error ASDISPLAYNODE_DEPRECATED_MSG("Please use didFailFetchingWithError: instead."); +- (void)imageNode:(ASNetworkImageNode *)imageNode didFailWithError:(NSError *)error; /** * Notification that the image node finished decoding an image. @@ -201,28 +201,6 @@ NS_ASSUME_NONNULL_BEGIN */ - (void)imageNodeDidFinishDecoding:(ASNetworkImageNode *)imageNode; -/** - * Notification that the image node finished downloading an image, with additional info. - * If implemented, this method will be called instead of `imageNode:didFetchImage:`. - * - * @param imageNode The sender. - * @param image The newly-loaded image. - * @param info Additional information about the image load. - * - * @discussion Called on a background queue. - */ -- (void)imageNode:(ASNetworkImageNode *)imageNode didFetchImage:(UIImage *)image info:(ASNetworkImageLoadInfo *)info; - -/** - * Notification that the image node failed to download the image. - * - * @param imageNode The sender. - * @param error The error with details. - * - * @discussion Called on a background queue. - */ -- (void)imageNode:(ASNetworkImageNode *)imageNode didFailFetchingWithError:(NSError *)error; - @end NS_ASSUME_NONNULL_END diff --git a/Source/ASNetworkImageNode.mm b/Source/ASNetworkImageNode.mm index 6d575c870..8733c84f5 100755 --- a/Source/ASNetworkImageNode.mm +++ b/Source/ASNetworkImageNode.mm @@ -111,11 +111,6 @@ - (instancetype)initWithCache:(id)cache downloader:(id)delegate _delegateFlags.delegateDidFinishDecoding = [delegate respondsToSelector:@selector(imageNodeDidFinishDecoding:)]; _delegateFlags.delegateDidLoadImage = [delegate respondsToSelector:@selector(imageNode:didLoadImage:)]; _delegateFlags.delegateDidLoadImageWithInfo = [delegate respondsToSelector:@selector(imageNode:didLoadImage:info:)]; - _delegateFlags.delegateDidFailFetchingWithError = [delegate respondsToSelector:@selector(imageNode:didFailFetchingWithError:)]; - _delegateFlags.delegateDidFetchImageWithInfo = [delegate respondsToSelector:@selector(imageNode:didFetchImage:info:)]; } - (id)delegate @@ -360,26 +353,13 @@ - (void)displayWillStartAsynchronously:(BOOL)asynchronously _imageLoaded = YES; // Call out to the delegate. - if (ASNetworkImageNode.delegateCallbacksOnMainThread && _delegateFlags.delegateDidLoadImageWithInfo) { + if (_delegateFlags.delegateDidLoadImageWithInfo) { ASUnlockScope(self); auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:url sourceType:ASNetworkImageSourceSynchronousCache downloadIdentifier:nil userInfo:nil]; -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" [_delegate imageNode:self didLoadImage:result info:info]; -#pragma clang diagnostic pop } else if (_delegateFlags.delegateDidLoadImage) { ASUnlockScope(self); -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" [_delegate imageNode:self didLoadImage:result]; -#pragma clang diagnostic pop - } - - // Call out to the delegate. - if (!ASNetworkImageNode.delegateCallbacksOnMainThread && _delegateFlags.delegateDidFetchImageWithInfo) { - ASUnlockScope(self); - auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:url sourceType:ASNetworkImageSourceSynchronousCache downloadIdentifier:nil userInfo:nil]; - [_delegate imageNode:self didFetchImage:result info:info]; } } } @@ -694,28 +674,13 @@ - (void)_lazilyLoadImageIfNecessary _imageLoaded = YES; [self _setCurrentImageQuality:1.0]; - if (ASNetworkImageNode.delegateCallbacksOnMainThread) { - if (_delegateFlags.delegateDidLoadImageWithInfo) { - ASUnlockScope(self); - auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:ASNetworkImageSourceFileURL downloadIdentifier:nil userInfo:nil]; - #pragma clang diagnostic push - #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [delegate imageNode:self didLoadImage:self.image info:info]; - #pragma clang diagnostic pop - } else if (_delegateFlags.delegateDidLoadImage) { - ASUnlockScope(self); - #pragma clang diagnostic push - #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [delegate imageNode:self didLoadImage:self.image]; - #pragma clang diagnostic pop - } - } else { - // Call out to the delegate. - if (_delegateFlags.delegateDidFetchImageWithInfo) { - ASUnlockScope(self); - auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:ASNetworkImageSourceFileURL downloadIdentifier:nil userInfo:nil]; - [_delegate imageNode:self didFetchImage:self.image info:info]; - } + if (_delegateFlags.delegateDidLoadImageWithInfo) { + ASUnlockScope(self); + auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:ASNetworkImageSourceFileURL downloadIdentifier:nil userInfo:nil]; + [delegate imageNode:self didLoadImage:self.image info:info]; + } else if (_delegateFlags.delegateDidLoadImage) { + ASUnlockScope(self); + [delegate imageNode:self didLoadImage:self.image]; } }); } else { @@ -763,43 +728,21 @@ - (void)_lazilyLoadImageIfNecessary void (^calloutBlock)(ASNetworkImageNode *inst); - if (ASNetworkImageNode.delegateCallbacksOnMainThread) { - if (newImage) { - if (_delegateFlags.delegateDidLoadImageWithInfo) { - calloutBlock = ^(ASNetworkImageNode *strongSelf) { - auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:imageSource downloadIdentifier:downloadIdentifier userInfo:userInfo]; - #pragma clang diagnostic push - #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [delegate imageNode:strongSelf didLoadImage:newImage info:info]; - #pragma clang diagnostic pop - }; - } else if (_delegateFlags.delegateDidLoadImage) { - calloutBlock = ^(ASNetworkImageNode *strongSelf) { - #pragma clang diagnostic push - #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [delegate imageNode:strongSelf didLoadImage:newImage]; - #pragma clang diagnostic pop - }; - } - } else if (error && _delegateFlags.delegateDidFailWithError) { - calloutBlock = ^(ASNetworkImageNode *strongSelf) { - #pragma clang diagnostic push - #pragma clang diagnostic ignored "-Wdeprecated-declarations" - [delegate imageNode:strongSelf didFailWithError:error]; - #pragma clang diagnostic pop - }; - } - } else { // !ASNetworkImageNode.delegateCallbacksOnMainThread - if (newImage && _delegateFlags.delegateDidFetchImageWithInfo) { + if (newImage) { + if (_delegateFlags.delegateDidLoadImageWithInfo) { calloutBlock = ^(ASNetworkImageNode *strongSelf) { auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:imageSource downloadIdentifier:downloadIdentifier userInfo:userInfo]; - [delegate imageNode:strongSelf didFetchImage:newImage info:info]; + [delegate imageNode:strongSelf didLoadImage:newImage info:info]; }; - } else if (error && _delegateFlags.delegateDidFailFetchingWithError) { + } else if (_delegateFlags.delegateDidLoadImage) { calloutBlock = ^(ASNetworkImageNode *strongSelf) { - [delegate imageNode:strongSelf didFailFetchingWithError:error]; + [delegate imageNode:strongSelf didLoadImage:newImage]; }; } + } else if (error && _delegateFlags.delegateDidFailWithError) { + calloutBlock = ^(ASNetworkImageNode *strongSelf) { + [delegate imageNode:strongSelf didFailWithError:error]; + }; } if (calloutBlock) { From 33a80df9b8a236bc65b94938e52d91f9ca2a57a5 Mon Sep 17 00:00:00 2001 From: Max Wang Date: Fri, 13 Jul 2018 11:03:23 -0700 Subject: [PATCH 10/16] Refactor --- Source/ASNetworkImageNode.h | 4 ++-- Source/ASNetworkImageNode.mm | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Source/ASNetworkImageNode.h b/Source/ASNetworkImageNode.h index 95a7cabc7..1cebc5277 100644 --- a/Source/ASNetworkImageNode.h +++ b/Source/ASNetworkImageNode.h @@ -161,7 +161,7 @@ NS_ASSUME_NONNULL_BEGIN * @param image The newly-loaded image. * @param info Additional information about the image load. * - * @discussion Called on a main queue. + * @discussion Called on the main thread if delegateCallbacksOnMainThread=YES (the default), otherwise on a background thread. */ - (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image info:(ASNetworkImageLoadInfo *)info; @@ -171,7 +171,7 @@ NS_ASSUME_NONNULL_BEGIN * @param imageNode The sender. * @param image The newly-loaded image. * - * @discussion Called on a main thread. + * @discussion Called on the main thread if delegateCallbacksOnMainThread=YES (the default), otherwise on a background thread. */ - (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image; diff --git a/Source/ASNetworkImageNode.mm b/Source/ASNetworkImageNode.mm index 8733c84f5..4881bb188 100755 --- a/Source/ASNetworkImageNode.mm +++ b/Source/ASNetworkImageNode.mm @@ -61,8 +61,6 @@ @interface ASNetworkImageNode () unsigned int delegateDidFinishDecoding:1; unsigned int delegateDidLoadImage:1; unsigned int delegateDidLoadImageWithInfo:1; - unsigned int delegateDidFailFetchingWithError:1; - unsigned int delegateDidFetchImageWithInfo:1; } _delegateFlags; @@ -674,6 +672,7 @@ - (void)_lazilyLoadImageIfNecessary _imageLoaded = YES; [self _setCurrentImageQuality:1.0]; + if (_delegateFlags.delegateDidLoadImageWithInfo) { ASUnlockScope(self); auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:ASNetworkImageSourceFileURL downloadIdentifier:nil userInfo:nil]; @@ -744,7 +743,7 @@ - (void)_lazilyLoadImageIfNecessary [delegate imageNode:strongSelf didFailWithError:error]; }; } - + if (calloutBlock) { if (ASNetworkImageNode.delegateCallbacksOnMainThread) { ASPerformBlockOnMainThread(^{ From 4134330f7afb1bd1e1c7717d75896c9fd1507243 Mon Sep 17 00:00:00 2001 From: Max Wang Date: Fri, 13 Jul 2018 11:05:50 -0700 Subject: [PATCH 11/16] refactor --- Source/ASNetworkImageNode.h | 2 +- Source/ASNetworkImageNode.mm | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/ASNetworkImageNode.h b/Source/ASNetworkImageNode.h index 1cebc5277..c1cf0b63e 100644 --- a/Source/ASNetworkImageNode.h +++ b/Source/ASNetworkImageNode.h @@ -190,7 +190,7 @@ NS_ASSUME_NONNULL_BEGIN * @param imageNode The sender. * @param error The error with details. * - * @discussion Called on a main thread. + * @discussion Called on the main thread if delegateCallbacksOnMainThread=YES (the default), otherwise on a background thread. */ - (void)imageNode:(ASNetworkImageNode *)imageNode didFailWithError:(NSError *)error; diff --git a/Source/ASNetworkImageNode.mm b/Source/ASNetworkImageNode.mm index 4881bb188..9b8008a49 100755 --- a/Source/ASNetworkImageNode.mm +++ b/Source/ASNetworkImageNode.mm @@ -726,7 +726,7 @@ - (void)_lazilyLoadImageIfNecessary strongSelf->_cacheSentinel++; void (^calloutBlock)(ASNetworkImageNode *inst); - + if (newImage) { if (_delegateFlags.delegateDidLoadImageWithInfo) { calloutBlock = ^(ASNetworkImageNode *strongSelf) { From f0419c290d88293acc46af9524f858ec372609cc Mon Sep 17 00:00:00 2001 From: Max Wang Date: Fri, 13 Jul 2018 11:12:17 -0700 Subject: [PATCH 12/16] revert to use let --- CHANGELOG.md | 1 + Source/ASNetworkImageNode.mm | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2002cc0ad..c6e38985c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## master * Add your own contributions to the next release on the line below this with your name. +- [ASNetworkImageNode] Allow delegate methods to be called on background thread [Max Wang](https://github.com/wsdwsd0829) [#1007](https://github.com/TextureGroup/Texture/pull/1007) - [ASDisplayNode] Adds support for multiple interface state delegates. [Garrett Moon](https://github.com/garrettmoon) [#979](https://github.com/TextureGroup/Texture/pull/979) - [ASDataController] Add capability to renew supplementary views (update map) when size change from zero to non-zero.[Max Wang](https://github.com/wsdwsd0829) [#842](https://github.com/TextureGroup/Texture/pull/842) - Make `ASPerformMainThreadDeallocation` visible in C. [Adlai Holler](https://github.com/Adlai-Holler) diff --git a/Source/ASNetworkImageNode.mm b/Source/ASNetworkImageNode.mm index 9b8008a49..b4c47630a 100755 --- a/Source/ASNetworkImageNode.mm +++ b/Source/ASNetworkImageNode.mm @@ -353,7 +353,7 @@ - (void)displayWillStartAsynchronously:(BOOL)asynchronously // Call out to the delegate. if (_delegateFlags.delegateDidLoadImageWithInfo) { ASUnlockScope(self); - auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:url sourceType:ASNetworkImageSourceSynchronousCache downloadIdentifier:nil userInfo:nil]; + let info = [[ASNetworkImageLoadInfo alloc] initWithURL:url sourceType:ASNetworkImageSourceSynchronousCache downloadIdentifier:nil userInfo:nil]; [_delegate imageNode:self didLoadImage:result info:info]; } else if (_delegateFlags.delegateDidLoadImage) { ASUnlockScope(self); @@ -675,7 +675,7 @@ - (void)_lazilyLoadImageIfNecessary if (_delegateFlags.delegateDidLoadImageWithInfo) { ASUnlockScope(self); - auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:ASNetworkImageSourceFileURL downloadIdentifier:nil userInfo:nil]; + let info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:ASNetworkImageSourceFileURL downloadIdentifier:nil userInfo:nil]; [delegate imageNode:self didLoadImage:self.image info:info]; } else if (_delegateFlags.delegateDidLoadImage) { ASUnlockScope(self); @@ -726,11 +726,11 @@ - (void)_lazilyLoadImageIfNecessary strongSelf->_cacheSentinel++; void (^calloutBlock)(ASNetworkImageNode *inst); - + if (newImage) { if (_delegateFlags.delegateDidLoadImageWithInfo) { calloutBlock = ^(ASNetworkImageNode *strongSelf) { - auto info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:imageSource downloadIdentifier:downloadIdentifier userInfo:userInfo]; + let info = [[ASNetworkImageLoadInfo alloc] initWithURL:URL sourceType:imageSource downloadIdentifier:downloadIdentifier userInfo:userInfo]; [delegate imageNode:strongSelf didLoadImage:newImage info:info]; }; } else if (_delegateFlags.delegateDidLoadImage) { From 0a0dbccb752fd1a0519713f8d3f157250d54a7f5 Mon Sep 17 00:00:00 2001 From: Max Wang Date: Fri, 13 Jul 2018 11:35:14 -0700 Subject: [PATCH 13/16] refactor --- Source/ASNetworkImageNode.h | 8 ++++---- Source/ASNetworkImageNode.mm | 16 +++++++++------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Source/ASNetworkImageNode.h b/Source/ASNetworkImageNode.h index c1cf0b63e..4722c88d2 100644 --- a/Source/ASNetworkImageNode.h +++ b/Source/ASNetworkImageNode.h @@ -59,7 +59,7 @@ NS_ASSUME_NONNULL_BEGIN /** * The delegate will receive callbacks on main thread. Default to YES. */ -@property (class) BOOL delegateCallbacksOnMainThread; +@property (class) BOOL useMainThreadDelegateCallbacks; /** * The image to display. @@ -161,7 +161,7 @@ NS_ASSUME_NONNULL_BEGIN * @param image The newly-loaded image. * @param info Additional information about the image load. * - * @discussion Called on the main thread if delegateCallbacksOnMainThread=YES (the default), otherwise on a background thread. + * @discussion Called on the main thread if useMainThreadDelegateCallbacks=YES (the default), otherwise on a background thread. */ - (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image info:(ASNetworkImageLoadInfo *)info; @@ -171,7 +171,7 @@ NS_ASSUME_NONNULL_BEGIN * @param imageNode The sender. * @param image The newly-loaded image. * - * @discussion Called on the main thread if delegateCallbacksOnMainThread=YES (the default), otherwise on a background thread. + * @discussion Called on the main thread if useMainThreadDelegateCallbacks=YES (the default), otherwise on a background thread. */ - (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image; @@ -190,7 +190,7 @@ NS_ASSUME_NONNULL_BEGIN * @param imageNode The sender. * @param error The error with details. * - * @discussion Called on the main thread if delegateCallbacksOnMainThread=YES (the default), otherwise on a background thread. + * @discussion Called on the main thread if useMainThreadDelegateCallbacks=YES (the default), otherwise on a background thread. */ - (void)imageNode:(ASNetworkImageNode *)imageNode didFailWithError:(NSError *)error; diff --git a/Source/ASNetworkImageNode.mm b/Source/ASNetworkImageNode.mm index b4c47630a..f3d43acf5 100755 --- a/Source/ASNetworkImageNode.mm +++ b/Source/ASNetworkImageNode.mm @@ -85,7 +85,7 @@ @interface ASNetworkImageNode () @implementation ASNetworkImageNode -static BOOL _delegateCallbacksOnMainThread = YES; +static BOOL _useMainThreadDelegateCallbacks = YES; @dynamic image; @@ -428,12 +428,14 @@ - (void)didEnterPreloadState [self _lazilyLoadImageIfNecessary]; } -+ (void)setDelegateCallbacksOnMainThread:(BOOL)delegateCallbacksOnMainThread { - _delegateCallbacksOnMainThread = delegateCallbacksOnMainThread; ++ (void)setUseMainThreadDelegateCallbacks:(BOOL)useMainThreadDelegateCallbacks +{ + _useMainThreadDelegateCallbacks = useMainThreadDelegateCallbacks; } -+ (BOOL)delegateCallbacksOnMainThread { - return _delegateCallbacksOnMainThread; ++ (BOOL)useMainThreadDelegateCallbacks +{ + return _useMainThreadDelegateCallbacks; } #pragma mark - Progress @@ -726,7 +728,7 @@ - (void)_lazilyLoadImageIfNecessary strongSelf->_cacheSentinel++; void (^calloutBlock)(ASNetworkImageNode *inst); - + if (newImage) { if (_delegateFlags.delegateDidLoadImageWithInfo) { calloutBlock = ^(ASNetworkImageNode *strongSelf) { @@ -745,7 +747,7 @@ - (void)_lazilyLoadImageIfNecessary } if (calloutBlock) { - if (ASNetworkImageNode.delegateCallbacksOnMainThread) { + if (ASNetworkImageNode.useMainThreadDelegateCallbacks) { ASPerformBlockOnMainThread(^{ if (auto strongSelf = weakSelf) { calloutBlock(strongSelf); From c2d8d9bc386af7bb17ebe3ee38b497799b17465d Mon Sep 17 00:00:00 2001 From: Max Wang Date: Wed, 18 Jul 2018 14:16:39 -0700 Subject: [PATCH 14/16] make class property atomic. --- Source/ASNetworkImageNode.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/ASNetworkImageNode.mm b/Source/ASNetworkImageNode.mm index f3d43acf5..543606e8f 100755 --- a/Source/ASNetworkImageNode.mm +++ b/Source/ASNetworkImageNode.mm @@ -85,7 +85,7 @@ @interface ASNetworkImageNode () @implementation ASNetworkImageNode -static BOOL _useMainThreadDelegateCallbacks = YES; +static std::atomic_bool _useMainThreadDelegateCallbacks(true); @dynamic image; From 056a3d9066fe44352e20a3292f0bce567bb9cae3 Mon Sep 17 00:00:00 2001 From: Max Wang Date: Wed, 18 Jul 2018 14:38:58 -0700 Subject: [PATCH 15/16] kick of new ci test. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dde8f67c6..24416ef8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## master * Add your own contributions to the next release on the line below this with your name. -- [ASNetworkImageNode] Allow delegate methods to be called on background thread [Max Wang](https://github.com/wsdwsd0829) [#1007](https://github.com/TextureGroup/Texture/pull/1007) +- [ASNetworkImageNode] Allow delegate methods to be called on background thread. [Max Wang](https://github.com/wsdwsd0829) [#1007](https://github.com/TextureGroup/Texture/pull/1007) - [ASLayoutTransition] Add support for preserving order after node moves during transitions. (This order defines the z-order as well.) [Kevin Smith](https://github.com/wiseoldduck) [#1006] - [ASDisplayNode] Adds support for multiple interface state delegates. [Garrett Moon](https://github.com/garrettmoon) [#979](https://github.com/TextureGroup/Texture/pull/979) - [ASDataController] Add capability to renew supplementary views (update map) when size change from zero to non-zero.[Max Wang](https://github.com/wsdwsd0829) [#842](https://github.com/TextureGroup/Texture/pull/842) From 645adce1aa187cd583f7cff90b5bda047f6b573b Mon Sep 17 00:00:00 2001 From: Max Wang Date: Mon, 23 Jul 2018 23:06:33 -0700 Subject: [PATCH 16/16] kick off new ci --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1011f136b..9e34d20c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## master * Add your own contributions to the next release on the line below this with your name. -- [ASNetworkImageNode] Allow delegate methods to be called on background thread. [Max Wang](https://github.com/wsdwsd0829) [#1007](https://github.com/TextureGroup/Texture/pull/1007) +- [ASNetworkImageNode] Allow delegate methods to be called on background thread. [Max Wang](https://github.com/wsdwsd0829). [#1007](https://github.com/TextureGroup/Texture/pull/1007) - [ASLayoutTransition] Add support for preserving order after node moves during transitions. (This order defines the z-order as well.) [Kevin Smith](https://github.com/wiseoldduck) [#1006] - [ASDisplayNode] Adds support for multiple interface state delegates. [Garrett Moon](https://github.com/garrettmoon) [#979](https://github.com/TextureGroup/Texture/pull/979) - [ASDataController] Add capability to renew supplementary views (update map) when size change from zero to non-zero.[Max Wang](https://github.com/wsdwsd0829) [#842](https://github.com/TextureGroup/Texture/pull/842)