SDWebImage (5.1.0)UIView+WebCacheOperation(六)

#import "UIView+WebCacheOperation.h"
#import "objc/runtime.h"

static char loadOperationKey;

// key is strong, value is weak because operation instance is retained by SDWebImageManager's runningOperations property
// we should use lock to keep thread-safe because these method may not be acessed from main queue
typedef NSMapTable<NSString *, id<SDWebImageOperation>> SDOperationsDictionary;

@implementation UIView (WebCacheOperation)

//获取任务字典NSMapTable,将NSMapTable关联对象到UIView
- (SDOperationsDictionary *)sd_operationDictionary {
    @synchronized(self) {
        //如果取到operations就返回否则就创建,保证线程安全
        SDOperationsDictionary *operations = objc_getAssociatedObject(self, &loadOperationKey);
        if (operations) {
            return operations;
        }
        //https://www.jianshu.com/p/cf4e15b26f64
        //NSHashTable与NSMapTable
        //用NSMapTable保存operation,NSMapTable可以设置value为弱引用,key为强引用
        operations = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsWeakMemory capacity:0];
        objc_setAssociatedObject(self, &loadOperationKey, operations, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        return operations;
    }
}

//通过key去拿operation
- (nullable id<SDWebImageOperation>)sd_imageLoadOperationForKey:(nullable NSString *)key  {
    id<SDWebImageOperation> operation;
    if (key) {
        SDOperationsDictionary *operationDictionary = [self sd_operationDictionary];
        @synchronized (self) {
            operation = [operationDictionary objectForKey:key];
        }
    }
    return operation;
}

//设置key和value
- (void)sd_setImageLoadOperation:(nullable id<SDWebImageOperation>)operation forKey:(nullable NSString *)key {
    if (key) {
        //先把之前的key对应的operation给删除了
        [self sd_cancelImageLoadOperationWithKey:key];
        //然后添加新的operation
        if (operation) {
            SDOperationsDictionary *operationDictionary = [self sd_operationDictionary];
            @synchronized (self) {
                [operationDictionary setObject:operation forKey:key];
            }
        }
    }
}

//通过key把operation给取出来,然后调用cancel方法,取消下载
- (void)sd_cancelImageLoadOperationWithKey:(nullable NSString *)key {
    if (key) {
        // Cancel in progress downloader from queue
        SDOperationsDictionary *operationDictionary = [self sd_operationDictionary];
        id<SDWebImageOperation> operation;
        
        @synchronized (self) {
            operation = [operationDictionary objectForKey:key];
        }
        if (operation) {
            if ([operation conformsToProtocol:@protocol(SDWebImageOperation)]) {
                [operation cancel];
            }
            @synchronized (self) {
                [operationDictionary removeObjectForKey:key];
            }
        }
    }
}

- (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key {
    if (key) {
        SDOperationsDictionary *operationDictionary = [self sd_operationDictionary];
        @synchronized (self) {
            [operationDictionary removeObjectForKey:key];
        }
    }
}

@end

sd给每个UIView都绑定了一个NSMapTable用于存放当前的下载任务,这就解决了tableview在滑动的时候当cell复用时,image不会错位的问题,这个分类的作用就是解决错位的问题的,当我们给一个imageview下载图片时候,首先去NSMapTable里把当前正在执行的operation给cancel掉然后从NSMapTable移除,然后把新的任务加入到NSMapTable中,这是面试经常被问到的一个考点。

https://sdwebimage.github.io/Categories/UIView(WebCacheOperation).html

然后我们研究下这个NSMapTable,sd为什么不用NSMutableDictionary呢,说明NSMapTable和NSMutableDictionary是有区别的。

key is strong, value is weak because operation instance is retained by SDWebImageManager's runningOperations property we should use lock to keep thread-safe because these method may not be acessed from main queue
typedef NSMapTable<NSString *, id<SDWebImageOperation>> SDOperationsDictionary;

/ key很强,值很弱,因为SDWebImageManager的runningOperations属性保留了操作实例
所以NSMapTable保存的value是弱引用的,当任务完成后自动会从NSMapTable中移除

NSHashTable和NSMapTable相关文章

https://www.jianshu.com/p/39b57ecc99fe
https://objccn.io/issue-7-1/

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 整体架构 按照分组方式,可以分为几组 定义通用宏和方法 SDWebImageCompat: 宏定义和C语言的一些工...
    CharmecarWang阅读 724评论 0 0
  • 技术无极限,从菜鸟开始,从源码开始。 由于公司目前项目还是用OC写的项目,没有升级swift 所以暂时SDWebI...
    充满活力的早晨阅读 12,713评论 0 2
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,161评论 1 32
  • 原理(核心逻辑): UIImageView或者UIButton调用 经过层层调用后都会跳转到UIView (Web...
    nucky_lee阅读 185评论 0 2
  • SDWebImage 一个支持远程服务器图片加载缓存的库 功能简介 UIImageView,UIButton,MK...
    kwdx阅读 1,585评论 0 3