BU_SDWebImageDownloaderOperation.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "BU_SDWebImageDownloader.h"
  10. #import "BU_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, nullable) NSURLCredential *credential;
  33. @property (assign, nonatomic) double minimumProgressInterval;
  34. @end
  35. /**
  36. The download operation class for SDWebImageDownloader.
  37. */
  38. @interface BU_SDWebImageDownloaderOperation : NSOperation <SDWebImageDownloaderOperation>
  39. /**
  40. * The request used by the operation's task.
  41. */
  42. @property (strong, nonatomic, readonly, nullable) NSURLRequest *request;
  43. /**
  44. * The response returned by the operation's task.
  45. */
  46. @property (strong, nonatomic, readonly, nullable) NSURLResponse *response;
  47. /**
  48. * The operation's task
  49. */
  50. @property (strong, nonatomic, readonly, nullable) NSURLSessionTask *dataTask;
  51. /**
  52. * The credential used for authentication challenges in `-URLSession:task:didReceiveChallenge:completionHandler:`.
  53. *
  54. * This will be overridden by any shared credentials that exist for the username or password of the request URL, if present.
  55. */
  56. @property (strong, nonatomic, nullable) NSURLCredential *credential;
  57. /**
  58. * 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.
  59. * The value should be 0.0-1.0.
  60. * @note If you're using progressive decoding feature, this will also effect the image refresh rate.
  61. * @note This value may enhance the performance if you don't want progress callback too frequently.
  62. * Defaults to 0, which means each time we receive the new data from URLSession, we callback the progressBlock immediately.
  63. */
  64. @property (assign, nonatomic) double minimumProgressInterval;
  65. /**
  66. * The options for the receiver.
  67. */
  68. @property (assign, nonatomic, readonly) SDWebImageDownloaderOptions options;
  69. /**
  70. * The context for the receiver.
  71. */
  72. @property (copy, nonatomic, readonly, nullable) SDWebImageContext *context;
  73. /**
  74. * Initializes a `SDWebImageDownloaderOperation` object
  75. *
  76. * @see SDWebImageDownloaderOperation
  77. *
  78. * @param request the URL request
  79. * @param session the URL session in which this operation will run
  80. * @param options downloader options
  81. *
  82. * @return the initialized instance
  83. */
  84. - (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
  85. inSession:(nullable NSURLSession *)session
  86. options:(SDWebImageDownloaderOptions)options;
  87. /**
  88. * Initializes a `SDWebImageDownloaderOperation` object
  89. *
  90. * @see SDWebImageDownloaderOperation
  91. *
  92. * @param request the URL request
  93. * @param session the URL session in which this operation will run
  94. * @param options downloader options
  95. * @param context A context contains different options to perform specify changes or processes, see `BU_SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
  96. *
  97. * @return the initialized instance
  98. */
  99. - (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
  100. inSession:(nullable NSURLSession *)session
  101. options:(SDWebImageDownloaderOptions)options
  102. context:(nullable SDWebImageContext *)context NS_DESIGNATED_INITIALIZER;
  103. /**
  104. * Adds handlers for progress and completion. Returns a tokent that can be passed to -cancel: to cancel this set of
  105. * callbacks.
  106. *
  107. * @param progressBlock the block executed when a new chunk of data arrives.
  108. * @note the progress block is executed on a background queue
  109. * @param completedBlock the block executed when the download is done.
  110. * @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
  111. *
  112. * @return the token to use to cancel this set of handlers
  113. */
  114. - (nullable id)addHandlersForProgress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  115. completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
  116. /**
  117. * Cancels a set of callbacks. Once all callbacks are canceled, the operation is cancelled.
  118. *
  119. * @param token the token representing a set of callbacks to cancel
  120. *
  121. * @return YES if the operation was stopped because this was the last token to be canceled. NO otherwise.
  122. */
  123. - (BOOL)cancel:(nullable id)token;
  124. @end