Skip to content

Commit

Permalink
Background image load api (#1007)
Browse files Browse the repository at this point in the history
* fix SIMULATE_WEB_RESPONSE not imported #449

* Fix to make rangeMode update in right time

* remove uncessary assert

* add api to allow delegated calls in background.

* fix typo

* 1. Add class property to decide whether to send delegate callbacks on
main or background.
2. remove non-info api.

* Refactor.

* add ivar for class property.

* Donot use extra api.

* Refactor

* refactor

* revert to use let

* refactor

* make class property atomic.

* kick of new ci test.

* kick off new ci
  • Loading branch information
wsdwsd0829 authored and nguyenhuy committed Jul 24, 2018
1 parent 95de2ab commit 905c582
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
- [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)
Expand Down
11 changes: 8 additions & 3 deletions Source/ASNetworkImageNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nullable, weak) id<ASNetworkImageNodeDelegate> delegate;

/**
* The delegate will receive callbacks on main thread. Default to YES.
*/
@property (class) BOOL useMainThreadDelegateCallbacks;

/**
* The image to display.
*
Expand Down Expand Up @@ -156,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 background queue.
* @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;

Expand All @@ -166,7 +171,7 @@ NS_ASSUME_NONNULL_BEGIN
* @param imageNode The sender.
* @param image The newly-loaded image.
*
* @discussion Called on a background queue.
* @discussion Called on the main thread if useMainThreadDelegateCallbacks=YES (the default), otherwise on a background thread.
*/
- (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image;

Expand All @@ -185,7 +190,7 @@ NS_ASSUME_NONNULL_BEGIN
* @param imageNode The sender.
* @param error The error with details.
*
* @discussion Called on a background queue.
* @discussion Called on the main thread if useMainThreadDelegateCallbacks=YES (the default), otherwise on a background thread.
*/
- (void)imageNode:(ASNetworkImageNode *)imageNode didFailWithError:(NSError *)error;

Expand Down
26 changes: 21 additions & 5 deletions Source/ASNetworkImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ @interface ASNetworkImageNode ()

@implementation ASNetworkImageNode

static std::atomic_bool _useMainThreadDelegateCallbacks(true);

@dynamic image;

- (instancetype)initWithCache:(id<ASImageCacheProtocol>)cache downloader:(id<ASImageDownloaderProtocol>)downloader
Expand Down Expand Up @@ -430,6 +432,16 @@ - (void)didEnterPreloadState
[self _lazilyLoadImageIfNecessary];
}

+ (void)setUseMainThreadDelegateCallbacks:(BOOL)useMainThreadDelegateCallbacks
{
_useMainThreadDelegateCallbacks = useMainThreadDelegateCallbacks;
}

+ (BOOL)useMainThreadDelegateCallbacks
{
return _useMainThreadDelegateCallbacks;
}

#pragma mark - Progress

- (void)handleProgressImage:(UIImage *)progressImage progress:(CGFloat)progress downloadIdentifier:(nullable id)downloadIdentifier
Expand Down Expand Up @@ -743,11 +755,15 @@ - (void)_lazilyLoadImageIfNecessary
}

if (calloutBlock) {
ASPerformBlockOnMainThread(^{
if (let strongSelf = weakSelf) {
calloutBlock(strongSelf);
}
});
if (ASNetworkImageNode.useMainThreadDelegateCallbacks) {
ASPerformBlockOnMainThread(^{
if (auto strongSelf = weakSelf) {
calloutBlock(strongSelf);
}
});
} else {
calloutBlock(self);
}
}
});
};
Expand Down

0 comments on commit 905c582

Please sign in to comment.