UIView+WebCacheOperation.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "UIView+WebCacheOperation.h"
  9. #if SD_UIKIT || SD_MAC
  10. #import "objc/runtime.h"
  11. static char loadOperationKey;
  12. typedef NSMutableDictionary<NSString *, id> SDOperationsDictionary;
  13. @implementation UIView (WebCacheOperation)
  14. - (SDOperationsDictionary *)operationDictionary {
  15. // SDOperationsDictionary *operations = objc_getAssociatedObject(self, &loadOperationKey);
  16. // if (operations) {
  17. // return operations;
  18. // }
  19. // operations = [NSMutableDictionary dictionary];
  20. // objc_setAssociatedObject(self, &loadOperationKey, operations, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  21. // return operations;
  22. @synchronized(self) {
  23. SDOperationsDictionary *operations = objc_getAssociatedObject(self, &loadOperationKey);
  24. if (operations) {
  25. return operations;
  26. }
  27. operations = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsWeakMemory capacity:0];
  28. objc_setAssociatedObject(self, &loadOperationKey, operations, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  29. return operations;
  30. }
  31. }
  32. - (void)sd_setImageLoadOperation:(nullable id)operation forKey:(nullable NSString *)key {
  33. // if (key) {
  34. // [self sd_cancelImageLoadOperationWithKey:key];
  35. // if (operation) {
  36. // SDOperationsDictionary *operationDictionary = [self operationDictionary];
  37. // operationDictionary[key] = operation;
  38. // }
  39. // }
  40. if (key) {
  41. [self sd_cancelImageLoadOperationWithKey:key];
  42. if (operation) {
  43. SDOperationsDictionary *operationDictionary = [self operationDictionary];
  44. @synchronized (self) {
  45. [operationDictionary setObject:operation forKey:key];
  46. }
  47. }
  48. }
  49. }
  50. - (void)sd_cancelImageLoadOperationWithKey:(nullable NSString *)key {
  51. // Cancel in progress downloader from queue
  52. // SDOperationsDictionary *operationDictionary = [self operationDictionary];
  53. // id operations = operationDictionary[key];
  54. // if (operations) {
  55. // if ([operations isKindOfClass:[NSArray class]]) {
  56. // for (id <SDWebImageOperation> operation in operations) {
  57. // if (operation) {
  58. // [operation cancel];
  59. // }
  60. // }
  61. // } else if ([operations conformsToProtocol:@protocol(SDWebImageOperation)]){
  62. // [(id<SDWebImageOperation>) operations cancel];
  63. // }
  64. // [operationDictionary removeObjectForKey:key];
  65. // }
  66. if (key) {
  67. // Cancel in progress downloader from queue
  68. SDOperationsDictionary *operationDictionary = [self operationDictionary];
  69. id<SDWebImageOperation> operation;
  70. @synchronized (self) {
  71. operation = [operationDictionary objectForKey:key];
  72. }
  73. if (operation) {
  74. if ([operation conformsToProtocol:@protocol(SDWebImageOperation)]) {
  75. [operation cancel];
  76. }
  77. @synchronized (self) {
  78. [operationDictionary removeObjectForKey:key];
  79. }
  80. }
  81. }
  82. }
  83. - (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key {
  84. if (key) {
  85. SDOperationsDictionary *operationDictionary = [self operationDictionary];
  86. [operationDictionary removeObjectForKey:key];
  87. }
  88. }
  89. @end
  90. #endif