Merge branch 'refactor_prefetcher' of https://github.com/dreampiggy/SDWebImage into 5.x

This commit is contained in:
DreamPiggy 2018-03-24 20:36:03 +08:00
commit 1efc247400
2 changed files with 21 additions and 5 deletions

View File

@ -13,6 +13,14 @@
@interface SDWebImagePrefetchToken : NSObject <SDWebImageOperation> @interface SDWebImagePrefetchToken : NSObject <SDWebImageOperation>
/**
* Cancel the current prefeching.
*/
- (void)cancel;
/**
list of URLs of current prefetching.
*/
@property (nonatomic, copy, readonly, nullable) NSArray<NSURL *> *urls; @property (nonatomic, copy, readonly, nullable) NSArray<NSURL *> *urls;
@end @end

View File

@ -17,7 +17,7 @@
@property (nonatomic, copy, readwrite) NSArray<NSURL *> *urls; @property (nonatomic, copy, readwrite) NSArray<NSURL *> *urls;
@property (nonatomic, assign) int64_t totalCount; @property (nonatomic, assign) int64_t totalCount;
@property (nonatomic, strong) NSMutableArray<id<SDWebImageOperation>> *operations; @property (nonatomic, strong) NSPointerArray *operations;
@property (nonatomic, weak) SDWebImagePrefetcher *prefetcher; @property (nonatomic, weak) SDWebImagePrefetcher *prefetcher;
@property (nonatomic, copy, nullable) SDWebImagePrefetcherCompletionBlock completionBlock; @property (nonatomic, copy, nullable) SDWebImagePrefetcherCompletionBlock completionBlock;
@property (nonatomic, copy, nullable) SDWebImagePrefetcherProgressBlock progressBlock; @property (nonatomic, copy, nullable) SDWebImagePrefetcherProgressBlock progressBlock;
@ -85,11 +85,12 @@
token->_finishedCount = 0; token->_finishedCount = 0;
token.urls = urls; token.urls = urls;
token.totalCount = urls.count; token.totalCount = urls.count;
token.operations = [NSMutableArray arrayWithCapacity:urls.count]; token.operations = [NSPointerArray weakObjectsPointerArray];
token.progressBlock = progressBlock; token.progressBlock = progressBlock;
token.completionBlock = completionBlock; token.completionBlock = completionBlock;
[self addRunningToken:token]; [self addRunningToken:token];
NSPointerArray *operations = token.operations;
for (NSURL *url in urls) { for (NSURL *url in urls) {
__weak typeof(self) wself = self; __weak typeof(self) wself = self;
id<SDWebImageOperation> operation = [self.manager loadImageWithURL:url options:self.options progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) { id<SDWebImageOperation> operation = [self.manager loadImageWithURL:url options:self.options progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
@ -115,7 +116,9 @@
[sself removeRunningToken:token]; [sself removeRunningToken:token];
} }
} context:self.context]; } context:self.context];
[token.operations addObject:operation]; @synchronized (token) {
[operations addPointer:(__bridge void *)operation];
}
} }
return token; return token;
@ -223,10 +226,15 @@
- (void)cancel { - (void)cancel {
@synchronized (self) { @synchronized (self) {
for (id<SDWebImageOperation> operation in self.operations) { for (id operation in self.operations) {
[operation cancel]; if ([operation conformsToProtocol:@protocol(SDWebImageOperation)]) {
[operation cancel];
}
} }
self.operations.count = 0;
} }
self.completionBlock = nil;
self.progressBlock = nil;
[self.prefetcher removeRunningToken:self]; [self.prefetcher removeRunningToken:self];
} }