SDWebImageDownloaderOperation.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 <Foundation/Foundation.h>
  9. #import "SDWebImageDownloader.h"
  10. #import "SDWebImageOperation.h"
  11. /**
  12. Describes a downloader operation. If one wants to use a custom downloader op, it needs to inherit from `NSOperation` and conform to this protocol
  13. For the description about these methods, see `SDWebImageDownloaderOperation`
  14. @note If your custom operation class does not use `NSURLSession` at all, do not implement the optional methods and session delegate methods.
  15. */
  16. @protocol SDWebImageDownloaderOperation <NSURLSessionTaskDelegate, NSURLSessionDataDelegate>
  17. @required
  18. - (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
  19. inSession:(nullable NSURLSession *)session
  20. options:(SDWebImageDownloaderOptions)options;
  21. - (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
  22. inSession:(nullable NSURLSession *)session
  23. options:(SDWebImageDownloaderOptions)options
  24. context:(nullable SDWebImageContext *)context;
  25. - (nullable id)addHandlersForProgress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  26. completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
  27. - (BOOL)cancel:(nullable id)token;
  28. @property (strong, nonatomic, readonly, nullable) NSURLRequest *request;
  29. @property (strong, nonatomic, readonly, nullable) NSURLResponse *response;
  30. @optional
  31. @property (strong, nonatomic, readonly, nullable) NSURLSessionTask *dataTask;
  32. @property (strong, nonatomic, readonly, nullable) NSURLSessionTaskMetrics *metrics API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
  33. @property (strong, nonatomic, nullable) NSURLCredential *credential;
  34. @property (assign, nonatomic) double minimumProgressInterval;
  35. @end
  36. /**
  37. The download operation class for SDWebImageDownloader.
  38. */
  39. @interface SDWebImageDownloaderOperation : NSOperation <SDWebImageDownloaderOperation>
  40. /**
  41. * The request used by the operation's task.
  42. */
  43. @property (strong, nonatomic, readonly, nullable) NSURLRequest *request;
  44. /**
  45. * The response returned by the operation's task.
  46. */
  47. @property (strong, nonatomic, readonly, nullable) NSURLResponse *response;
  48. /**
  49. * The operation's task
  50. */
  51. @property (strong, nonatomic, readonly, nullable) NSURLSessionTask *dataTask;
  52. /**
  53. * The collected metrics from `-URLSession:task:didFinishCollectingMetrics:`.
  54. * This can be used to collect the network metrics like download duration, DNS lookup duration, SSL handshake duration, etc. See Apple's documentation: https://developer.apple.com/documentation/foundation/urlsessiontaskmetrics
  55. */
  56. @property (strong, nonatomic, readonly, nullable) NSURLSessionTaskMetrics *metrics API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
  57. /**
  58. * The credential used for authentication challenges in `-URLSession:task:didReceiveChallenge:completionHandler:`.
  59. *
  60. * This will be overridden by any shared credentials that exist for the username or password of the request URL, if present.
  61. */
  62. @property (strong, nonatomic, nullable) NSURLCredential *credential;
  63. /**
  64. * The minimum interval about progress percent during network downloading. Which means the next progress callback and current progress callback's progress percent difference should be larger or equal to this value. However, the final finish download progress callback does not get effected.
  65. * The value should be 0.0-1.0.
  66. * @note If you're using progressive decoding feature, this will also effect the image refresh rate.
  67. * @note This value may enhance the performance if you don't want progress callback too frequently.
  68. * Defaults to 0, which means each time we receive the new data from URLSession, we callback the progressBlock immediately.
  69. */
  70. @property (assign, nonatomic) double minimumProgressInterval;
  71. /**
  72. * The options for the receiver.
  73. */
  74. @property (assign, nonatomic, readonly) SDWebImageDownloaderOptions options;
  75. /**
  76. * The context for the receiver.
  77. */
  78. @property (copy, nonatomic, readonly, nullable) SDWebImageContext *context;
  79. /**
  80. * Initializes a `SDWebImageDownloaderOperation` object
  81. *
  82. * @see SDWebImageDownloaderOperation
  83. *
  84. * @param request the URL request
  85. * @param session the URL session in which this operation will run
  86. * @param options downloader options
  87. *
  88. * @return the initialized instance
  89. */
  90. - (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
  91. inSession:(nullable NSURLSession *)session
  92. options:(SDWebImageDownloaderOptions)options;
  93. /**
  94. * Initializes a `SDWebImageDownloaderOperation` object
  95. *
  96. * @see SDWebImageDownloaderOperation
  97. *
  98. * @param request the URL request
  99. * @param session the URL session in which this operation will run
  100. * @param options downloader options
  101. * @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
  102. *
  103. * @return the initialized instance
  104. */
  105. - (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
  106. inSession:(nullable NSURLSession *)session
  107. options:(SDWebImageDownloaderOptions)options
  108. context:(nullable SDWebImageContext *)context NS_DESIGNATED_INITIALIZER;
  109. /**
  110. * Adds handlers for progress and completion. Returns a tokent that can be passed to -cancel: to cancel this set of
  111. * callbacks.
  112. *
  113. * @param progressBlock the block executed when a new chunk of data arrives.
  114. * @note the progress block is executed on a background queue
  115. * @param completedBlock the block executed when the download is done.
  116. * @note the completed block is executed on the main queue for success. If errors are found, there is a chance the block will be executed on a background queue
  117. *
  118. * @return the token to use to cancel this set of handlers
  119. */
  120. - (nullable id)addHandlersForProgress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  121. completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
  122. /**
  123. * Cancels a set of callbacks. Once all callbacks are canceled, the operation is cancelled.
  124. *
  125. * @param token the token representing a set of callbacks to cancel
  126. *
  127. * @return YES if the operation was stopped because this was the last token to be canceled. NO otherwise.
  128. */
  129. - (BOOL)cancel:(nullable id)token;
  130. @end