#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)
//獲取任務(wù)字典NSMapTable,將NSMapTable關(guān)聯(lián)對(duì)象到UIView
- (SDOperationsDictionary *)sd_operationDictionary {
@synchronized(self) {
//如果取到operations就返回否則就創(chuàng)建,保證線程安全
SDOperationsDictionary *operations = objc_getAssociatedObject(self, &loadOperationKey);
if (operations) {
return operations;
}
//http://www.reibang.com/p/cf4e15b26f64
//NSHashTable與NSMapTable
//用NSMapTable保存operation,NSMapTable可以設(shè)置value為弱引用,key為強(qiáng)引用
operations = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsWeakMemory capacity:0];
objc_setAssociatedObject(self, &loadOperationKey, operations, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return operations;
}
}
//通過(guò)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;
}
//設(shè)置key和value
- (void)sd_setImageLoadOperation:(nullable id<SDWebImageOperation>)operation forKey:(nullable NSString *)key {
if (key) {
//先把之前的key對(duì)應(yīng)的operation給刪除了
[self sd_cancelImageLoadOperationWithKey:key];
//然后添加新的operation
if (operation) {
SDOperationsDictionary *operationDictionary = [self sd_operationDictionary];
@synchronized (self) {
[operationDictionary setObject:operation forKey:key];
}
}
}
}
//通過(guò)key把operation給取出來(lái),然后調(diào)用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給每個(gè)UIView都綁定了一個(gè)NSMapTable用于存放當(dāng)前的下載任務(wù),這就解決了tableview在滑動(dòng)的時(shí)候當(dāng)cell復(fù)用時(shí),image不會(huì)錯(cuò)位的問(wèn)題贤徒,這個(gè)分類的作用就是解決錯(cuò)位的問(wèn)題的,當(dāng)我們給一個(gè)imageview下載圖片時(shí)候,首先去NSMapTable里把當(dāng)前正在執(zhí)行的operation給cancel掉然后從NSMapTable移除脖咐,然后把新的任務(wù)加入到NSMapTable中,這是面試經(jīng)常被問(wèn)到的一個(gè)考點(diǎn)汇歹。
https://sdwebimage.github.io/Categories/UIView(WebCacheOperation).html
然后我們研究下這個(gè)NSMapTable屁擅,sd為什么不用NSMutableDictionary呢,說(shuō)明NSMapTable和NSMutableDictionary是有區(qū)別的产弹。
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很強(qiáng)派歌,值很弱,因?yàn)镾DWebImageManager的runningOperations屬性保留了操作實(shí)例
所以NSMapTable保存的value是弱引用的,當(dāng)任務(wù)完成后自動(dòng)會(huì)從NSMapTable中移除
NSHashTable和NSMapTable相關(guān)文章
http://www.reibang.com/p/39b57ecc99fe
https://objccn.io/issue-7-1/