Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background image load api #1007

Merged
merged 36 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
929d118
fix SIMULATE_WEB_RESPONSE not imported #449
wsdwsd0829 Jul 16, 2017
dd24d8f
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Sep 6, 2017
b8eaffa
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Oct 4, 2017
2918ea0
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Oct 11, 2017
9c42266
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Oct 12, 2017
329f35f
Fix to make rangeMode update in right time
wsdwsd0829 Feb 5, 2018
5f8b7ec
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Feb 7, 2018
d87bb11
merge master from upstream
wsdwsd0829 Feb 15, 2018
269c2ab
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Mar 10, 2018
24c1ce8
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Mar 12, 2018
233169e
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Mar 26, 2018
b19f90d
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Mar 28, 2018
b50cec4
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Apr 5, 2018
b75a5f3
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 May 3, 2018
64b46e0
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 May 16, 2018
5fabc1e
remove uncessary assert
wsdwsd0829 May 17, 2018
3d5b84b
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 May 21, 2018
098b978
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Jun 8, 2018
77eefd6
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Jun 13, 2018
0310ed7
Merge branch 'master' of github.com:TextureGroup/Texture
wsdwsd0829 Jul 3, 2018
324d3b0
add api to allow delegated calls in background.
wsdwsd0829 Jul 4, 2018
018867e
fix typo
wsdwsd0829 Jul 4, 2018
1da91a6
1. Add class property to decide whether to send delegate callbacks on
wsdwsd0829 Jul 12, 2018
bdbd7de
Refactor.
wsdwsd0829 Jul 12, 2018
8480a75
add ivar for class property.
wsdwsd0829 Jul 12, 2018
30fed6b
merge masster
wsdwsd0829 Jul 12, 2018
c17c0a1
Donot use extra api.
wsdwsd0829 Jul 13, 2018
33a80df
Refactor
wsdwsd0829 Jul 13, 2018
4134330
refactor
wsdwsd0829 Jul 13, 2018
f0419c2
revert to use let
wsdwsd0829 Jul 13, 2018
6d1ce31
Merge branch 'master' into background-image-load-api
wsdwsd0829 Jul 13, 2018
0a0dbcc
refactor
wsdwsd0829 Jul 13, 2018
c2d8d9b
make class property atomic.
wsdwsd0829 Jul 18, 2018
056a3d9
kick of new ci test.
wsdwsd0829 Jul 18, 2018
d6c133a
Merge branch 'master' of github.com:TextureGroup/Texture into backgro…
wsdwsd0829 Jul 18, 2018
645adce
kick off new ci
wsdwsd0829 Jul 24, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -85,6 +85,8 @@ @interface ASNetworkImageNode ()

@implementation ASNetworkImageNode

static BOOL _useMainThreadDelegateCallbacks = YES;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this should be static std::atomic_bool _useMainThreadDelegateCallbacks(true) or some variant. The other code in here should be OK, since c++ defines the operators to implicitly do store/load.

In practice the risk is trivial. I approve either way – let's get this landed!


@dynamic image;

- (instancetype)initWithCache:(id<ASImageCacheProtocol>)cache downloader:(id<ASImageDownloaderProtocol>)downloader
Expand Down Expand Up @@ -426,6 +428,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 @@ -735,11 +747,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