PINRemoteImageTask.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // PINRemoteImageTask.m
  3. // Pods
  4. //
  5. // Created by Garrett Moon on 3/9/15.
  6. //
  7. //
  8. #import "PINRemoteImageTask.h"
  9. #import "PINRemoteImageCallbacks.h"
  10. @implementation PINRemoteImageTask
  11. - (instancetype)init
  12. {
  13. if (self = [super init]) {
  14. self.callbackBlocks = [[NSMutableDictionary alloc] init];
  15. }
  16. return self;
  17. }
  18. - (NSString *)description
  19. {
  20. return [NSString stringWithFormat:@"<%@: %p> completionBlocks: %lu", NSStringFromClass([self class]), self, (unsigned long)self.callbackBlocks.count];
  21. }
  22. - (void)addCallbacksWithCompletionBlock:(PINRemoteImageManagerImageCompletion)completionBlock
  23. progressImageBlock:(PINRemoteImageManagerImageCompletion)progressImageBlock
  24. progressDownloadBlock:(PINRemoteImageManagerProgressDownload)progressDownloadBlock
  25. withUUID:(NSUUID *)UUID
  26. {
  27. PINRemoteImageCallbacks *completion = [[PINRemoteImageCallbacks alloc] init];
  28. completion.completionBlock = completionBlock;
  29. completion.progressImageBlock = progressImageBlock;
  30. completion.progressDownloadBlock = progressDownloadBlock;
  31. [self.callbackBlocks setObject:completion forKey:UUID];
  32. }
  33. - (void)removeCallbackWithUUID:(NSUUID *)UUID
  34. {
  35. [self.callbackBlocks removeObjectForKey:UUID];
  36. }
  37. - (void)callCompletionsWithQueue:(dispatch_queue_t)queue
  38. remove:(BOOL)remove
  39. withImage:(PINImage *)image
  40. alternativeRepresentation:(id)alternativeRepresentation
  41. cached:(BOOL)cached
  42. error:(NSError *)error
  43. {
  44. __weak typeof(self) weakSelf = self;
  45. [self.callbackBlocks enumerateKeysAndObjectsUsingBlock:^(NSUUID *UUID, PINRemoteImageCallbacks *callback, BOOL *stop) {
  46. typeof(self) strongSelf = weakSelf;
  47. if (callback.completionBlock != nil) {
  48. PINLog(@"calling completion for UUID: %@ key: %@", UUID, strongSelf.key);
  49. PINRemoteImageManagerImageCompletion completionBlock = callback.completionBlock;
  50. CFTimeInterval requestTime = callback.requestTime;
  51. //The code run asynchronously below is *not* guaranteed to be run in the manager's lock!
  52. //All access to the callbacks and self should be done outside the block below!
  53. dispatch_async(queue, ^
  54. {
  55. PINRemoteImageResultType result;
  56. if (image || alternativeRepresentation) {
  57. result = cached ? PINRemoteImageResultTypeCache : PINRemoteImageResultTypeDownload;
  58. } else {
  59. result = PINRemoteImageResultTypeNone;
  60. }
  61. completionBlock([PINRemoteImageManagerResult imageResultWithImage:image
  62. alternativeRepresentation:alternativeRepresentation
  63. requestLength:CACurrentMediaTime() - requestTime
  64. error:error
  65. resultType:result
  66. UUID:UUID]);
  67. });
  68. }
  69. if (remove) {
  70. [strongSelf removeCallbackWithUUID:UUID];
  71. }
  72. }];
  73. }
  74. - (BOOL)cancelWithUUID:(NSUUID *)UUID manager:(PINRemoteImageManager *)manager
  75. {
  76. BOOL noMoreCompletions = NO;
  77. [self removeCallbackWithUUID:UUID];
  78. if ([self.callbackBlocks count] == 0) {
  79. noMoreCompletions = YES;
  80. }
  81. return noMoreCompletions;
  82. }
  83. - (void)setPriority:(PINRemoteImageManagerPriority)priority
  84. {
  85. }
  86. @end