Added the new Async API for disk data query, to avoid user to dispatch their own global queu (not IO queue), solve the IO safe issue

This commit is contained in:
DreamPiggy 2020-04-01 17:12:11 +08:00
parent 7f540a6296
commit de153b0a32
3 changed files with 21 additions and 0 deletions

View File

@ -235,6 +235,15 @@ typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) {
*/
- (nullable NSData *)diskImageDataForKey:(nullable NSString *)key;
/**
* Asynchronously load the image data in disk cache. You can decode the image data to image after loaded.
*
* @param key The unique key used to store the wanted image
* @param completionBlock the block to be executed when the check is done.
* @note the completion block will be always executed on the main queue
*/
- (void)diskImageDataQueryForKey:(nullable NSString *)key completion:(nullable SDImageCacheQueryDataCompletionBlock)completionBlock;
/**
* Operation that queries the cache asynchronously and call the completion when done.
*

View File

@ -314,6 +314,17 @@
return [self.diskCache containsDataForKey:key];
}
- (void)diskImageDataQueryForKey:(NSString *)key completion:(SDImageCacheQueryDataCompletionBlock)completionBlock {
dispatch_async(self.ioQueue, ^{
NSData *imageData = [self diskImageDataBySearchingAllPathsForKey:key];
if (completionBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
completionBlock(imageData);
});
}
});
}
- (nullable NSData *)diskImageDataForKey:(nullable NSString *)key {
if (!key) {
return nil;

View File

@ -36,6 +36,7 @@ typedef NS_ENUM(NSInteger, SDImageCacheType) {
};
typedef void(^SDImageCacheCheckCompletionBlock)(BOOL isInCache);
typedef void(^SDImageCacheQueryDataCompletionBlock)(NSData * _Nullable data);
typedef void(^SDImageCacheCalculateSizeBlock)(NSUInteger fileCount, NSUInteger totalSize);
typedef NSString * _Nullable (^SDImageCacheAdditionalCachePathBlock)(NSString * _Nonnull key);
typedef void(^SDImageCacheQueryCompletionBlock)(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType);