SDWebImagePrefetcher.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "SDWebImageManager.h"
  10. @class SDWebImagePrefetcher;
  11. /**
  12. A token represents a list of URLs, can be used to cancel the download.
  13. */
  14. @interface SDWebImagePrefetchToken : NSObject <SDWebImageOperation>
  15. /**
  16. * Cancel the current prefetching.
  17. */
  18. - (void)cancel;
  19. /**
  20. list of URLs of current prefetching.
  21. */
  22. @property (nonatomic, copy, readonly, nullable) NSArray<NSURL *> *urls;
  23. @end
  24. /**
  25. The prefetcher delegate protocol
  26. */
  27. @protocol SDWebImagePrefetcherDelegate <NSObject>
  28. @optional
  29. /**
  30. * Called when an image was prefetched. Which means it's called when one URL from any of prefetching finished.
  31. *
  32. * @param imagePrefetcher The current image prefetcher
  33. * @param imageURL The image url that was prefetched
  34. * @param finishedCount The total number of images that were prefetched (successful or not)
  35. * @param totalCount The total number of images that were to be prefetched
  36. */
  37. - (void)imagePrefetcher:(nonnull SDWebImagePrefetcher *)imagePrefetcher didPrefetchURL:(nullable NSURL *)imageURL finishedCount:(NSUInteger)finishedCount totalCount:(NSUInteger)totalCount;
  38. /**
  39. * Called when all images are prefetched. Which means it's called when all URLs from all of prefetching finished.
  40. * @param imagePrefetcher The current image prefetcher
  41. * @param totalCount The total number of images that were prefetched (whether successful or not)
  42. * @param skippedCount The total number of images that were skipped
  43. */
  44. - (void)imagePrefetcher:(nonnull SDWebImagePrefetcher *)imagePrefetcher didFinishWithTotalCount:(NSUInteger)totalCount skippedCount:(NSUInteger)skippedCount;
  45. @end
  46. typedef void(^SDWebImagePrefetcherProgressBlock)(NSUInteger noOfFinishedUrls, NSUInteger noOfTotalUrls);
  47. typedef void(^SDWebImagePrefetcherCompletionBlock)(NSUInteger noOfFinishedUrls, NSUInteger noOfSkippedUrls);
  48. /**
  49. * Prefetch some URLs in the cache for future use. Images are downloaded in low priority.
  50. */
  51. @interface SDWebImagePrefetcher : NSObject
  52. /**
  53. * The web image manager used by prefetcher to prefetch images.
  54. * @note You can specify a standalone manager and downloader with custom configuration suitable for image prefetching. Such as `currentDownloadCount` or `downloadTimeout`.
  55. */
  56. @property (strong, nonatomic, readonly, nonnull) SDWebImageManager *manager;
  57. /**
  58. * Maximum number of URLs to prefetch at the same time. Defaults to 3.
  59. */
  60. @property (nonatomic, assign) NSUInteger maxConcurrentPrefetchCount;
  61. /**
  62. * The options for prefetcher. Defaults to SDWebImageLowPriority.
  63. * @deprecated Prefetcher is designed to be used shared and should not effect others. So in 5.15.0 we added API `prefetchURLs:options:context:`. If you want global control, try to use `SDWebImageOptionsProcessor` in manager level.
  64. */
  65. @property (nonatomic, assign) SDWebImageOptions options API_DEPRECATED("Use individual prefetch options param instead", macos(10.10, API_TO_BE_DEPRECATED), ios(8.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED));
  66. /**
  67. * The context for prefetcher. Defaults to nil.
  68. * @deprecated Prefetcher is designed to be used shared and should not effect others. So in 5.15.0 we added API `prefetchURLs:options:context:`. If you want global control, try to use `SDWebImageOptionsProcessor` in `SDWebImageManager.optionsProcessor`.
  69. */
  70. @property (nonatomic, copy, nullable) SDWebImageContext *context API_DEPRECATED("Use individual prefetch context param instead", macos(10.10, API_TO_BE_DEPRECATED), ios(8.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED));
  71. /**
  72. * Queue options for prefetcher when call the progressBlock, completionBlock and delegate methods. Defaults to Main Queue.
  73. * @deprecated 5.15.0 introduce SDCallbackQueue, use that is preferred and has higher priority. The set/get to this property will translate to that instead.
  74. * @note The call is asynchronously to avoid blocking target queue. (see SDCallbackPolicyDispatch)
  75. * @note The delegate queue should be set before any prefetching start and may not be changed during prefetching to avoid thread-safe problem.
  76. */
  77. @property (strong, nonatomic, nonnull) dispatch_queue_t delegateQueue API_DEPRECATED("Use SDWebImageContextCallbackQueue context param instead, see SDCallbackQueue", macos(10.10, 10.10), ios(8.0, 8.0), tvos(9.0, 9.0), watchos(2.0, 2.0));
  78. /**
  79. * The delegate for the prefetcher. Defaults to nil.
  80. */
  81. @property (weak, nonatomic, nullable) id <SDWebImagePrefetcherDelegate> delegate;
  82. /**
  83. * Returns the global shared image prefetcher instance. It use a standalone manager which is different from shared manager.
  84. */
  85. @property (nonatomic, class, readonly, nonnull) SDWebImagePrefetcher *sharedImagePrefetcher;
  86. /**
  87. * Allows you to instantiate a prefetcher with any arbitrary image manager.
  88. */
  89. - (nonnull instancetype)initWithImageManager:(nonnull SDWebImageManager *)manager NS_DESIGNATED_INITIALIZER;
  90. /**
  91. * Assign list of URLs to let SDWebImagePrefetcher to queue the prefetching. It based on the image manager so the image may from the cache and network according to the `options` property.
  92. * Prefetching is separate to each other, which means the progressBlock and completionBlock you provide is bind to the prefetching for the list of urls.
  93. * Attention that call this will not cancel previous fetched urls. You should keep the token return by this to cancel or cancel all the prefetch.
  94. *
  95. * @param urls list of URLs to prefetch
  96. * @return the token to cancel the current prefetching.
  97. */
  98. - (nullable SDWebImagePrefetchToken *)prefetchURLs:(nullable NSArray<NSURL *> *)urls;
  99. /**
  100. * Assign list of URLs to let SDWebImagePrefetcher to queue the prefetching. It based on the image manager so the image may from the cache and network according to the `options` property.
  101. * Prefetching is separate to each other, which means the progressBlock and completionBlock you provide is bind to the prefetching for the list of urls.
  102. * Attention that call this will not cancel previous fetched urls. You should keep the token return by this to cancel or cancel all the prefetch.
  103. *
  104. * @param urls list of URLs to prefetch
  105. * @param progressBlock block to be called when progress updates;
  106. * first parameter is the number of completed (successful or not) requests,
  107. * second parameter is the total number of images originally requested to be prefetched
  108. * @param completionBlock block to be called when the current prefetching is completed
  109. * first param is the number of completed (successful or not) requests,
  110. * second parameter is the number of skipped requests
  111. * @return the token to cancel the current prefetching.
  112. */
  113. - (nullable SDWebImagePrefetchToken *)prefetchURLs:(nullable NSArray<NSURL *> *)urls
  114. progress:(nullable SDWebImagePrefetcherProgressBlock)progressBlock
  115. completed:(nullable SDWebImagePrefetcherCompletionBlock)completionBlock;
  116. /**
  117. * Assign list of URLs to let SDWebImagePrefetcher to queue the prefetching. It based on the image manager so the image may from the cache and network according to the `options` property.
  118. * Prefetching is separate to each other, which means the progressBlock and completionBlock you provide is bind to the prefetching for the list of urls.
  119. * Attention that call this will not cancel previous fetched urls. You should keep the token return by this to cancel or cancel all the prefetch.
  120. *
  121. * @param urls list of URLs to prefetch
  122. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  123. * @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.
  124. * @param progressBlock block to be called when progress updates;
  125. * first parameter is the number of completed (successful or not) requests,
  126. * second parameter is the total number of images originally requested to be prefetched
  127. * @param completionBlock block to be called when the current prefetching is completed
  128. * first param is the number of completed (successful or not) requests,
  129. * second parameter is the number of skipped requests
  130. * @return the token to cancel the current prefetching.
  131. */
  132. - (nullable SDWebImagePrefetchToken *)prefetchURLs:(nullable NSArray<NSURL *> *)urls
  133. options:(SDWebImageOptions)options
  134. context:(nullable SDWebImageContext *)context
  135. progress:(nullable SDWebImagePrefetcherProgressBlock)progressBlock
  136. completed:(nullable SDWebImagePrefetcherCompletionBlock)completionBlock;
  137. /**
  138. * Remove and cancel all the prefeching for the prefetcher.
  139. */
  140. - (void)cancelPrefetching;
  141. @end