PINRemoteImageDownloadTask.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // PINRemoteImageDownloadTask.m
  3. // Pods
  4. //
  5. // Created by Garrett Moon on 3/9/15.
  6. //
  7. //
  8. #import "PINRemoteImageDownloadTask.h"
  9. #import "PINRemoteImage.h"
  10. #import "PINRemoteImageCallbacks.h"
  11. @interface PINRemoteImageDownloadTask () {
  12. BOOL _canSetDataTaskPriority;
  13. }
  14. @end
  15. @implementation PINRemoteImageDownloadTask
  16. - (instancetype)init
  17. {
  18. if (self = [super init]) {
  19. _canSetDataTaskPriority = [NSURLSessionTask instancesRespondToSelector:@selector(setPriority:)];
  20. _numberOfRetries = 0;
  21. }
  22. return self;
  23. }
  24. - (BOOL)hasProgressBlocks
  25. {
  26. __block BOOL hasProgressBlocks = NO;
  27. [self.callbackBlocks enumerateKeysAndObjectsUsingBlock:^(NSUUID *UUID, PINRemoteImageCallbacks *callback, BOOL *stop) {
  28. if (callback.progressImageBlock) {
  29. hasProgressBlocks = YES;
  30. *stop = YES;
  31. }
  32. }];
  33. return hasProgressBlocks;
  34. }
  35. - (void)callProgressDownloadWithQueue:(nonnull dispatch_queue_t)queue completedBytes:(int64_t)completedBytes totalBytes:(int64_t)totalBytes
  36. {
  37. [self.callbackBlocks enumerateKeysAndObjectsUsingBlock:^(NSUUID *UUID, PINRemoteImageCallbacks *callback, BOOL *stop) {
  38. if (callback.progressDownloadBlock != nil) {
  39. PINLog(@"calling progress for UUID: %@ key: %@", UUID, self.key);
  40. PINRemoteImageManagerProgressDownload progressDownloadBlock = callback.progressDownloadBlock;
  41. //The code run asynchronously below is *not* guaranteed to be run in the manager's lock!
  42. //All access to the callbacks and self should be done outside the block below!
  43. dispatch_async(queue, ^
  44. {
  45. progressDownloadBlock(completedBytes, totalBytes);
  46. });
  47. }
  48. }];
  49. }
  50. - (void)callProgressImageWithQueue:(nonnull dispatch_queue_t)queue withImage:(nonnull PINImage *)image renderedImageQuality:(CGFloat)renderedImageQuality
  51. {
  52. [self.callbackBlocks enumerateKeysAndObjectsUsingBlock:^(NSUUID *UUID, PINRemoteImageCallbacks *callback, BOOL *stop) {
  53. if (callback.progressImageBlock != nil) {
  54. PINLog(@"calling progress for UUID: %@ key: %@", UUID, self.key);
  55. PINRemoteImageManagerImageCompletion progressImageBlock = callback.progressImageBlock;
  56. CFTimeInterval requestTime = callback.requestTime;
  57. //The code run asynchronously below is *not* guaranteed to be run in the manager's lock!
  58. //All access to the callbacks and self should be done outside the block below!
  59. dispatch_async(queue, ^
  60. {
  61. progressImageBlock([PINRemoteImageManagerResult imageResultWithImage:image
  62. alternativeRepresentation:nil
  63. requestLength:CACurrentMediaTime() - requestTime
  64. error:nil
  65. resultType:PINRemoteImageResultTypeProgress
  66. UUID:UUID
  67. renderedImageQuality:renderedImageQuality]);
  68. });
  69. }
  70. }];
  71. }
  72. - (BOOL)cancelWithUUID:(NSUUID *)UUID manager:(PINRemoteImageManager *)manager
  73. {
  74. BOOL noMoreCompletions = [super cancelWithUUID:UUID manager:manager];
  75. if (noMoreCompletions) {
  76. [self.urlSessionTaskOperation cancel];
  77. PINLog(@"Canceling download of URL: %@, UUID: %@", self.urlSessionTaskOperation.dataTask.originalRequest.URL, UUID);
  78. } else {
  79. PINLog(@"Decrementing download of URL: %@, UUID: %@", self.urlSessionTaskOperation.dataTask.originalRequest.URL, UUID);
  80. }
  81. return noMoreCompletions;
  82. }
  83. - (void)setPriority:(PINRemoteImageManagerPriority)priority
  84. {
  85. [super setPriority:priority];
  86. if (_canSetDataTaskPriority) {
  87. self.urlSessionTaskOperation.dataTask.priority = dataTaskPriorityWithImageManagerPriority(priority);
  88. }
  89. self.urlSessionTaskOperation.queuePriority = operationPriorityWithImageManagerPriority(priority);
  90. }
  91. @end