ImagePrefetcher.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. //
  2. // ImagePrefetcher.swift
  3. // Kingfisher
  4. //
  5. // Created by Claire Knight <claire.knight@moggytech.co.uk> on 24/02/2016
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #if os(macOS)
  27. import AppKit
  28. #else
  29. import UIKit
  30. #endif
  31. /// Progress update block of prefetcher when initialized with a list of resources.
  32. ///
  33. /// - `skippedResources`: An array of resources that are already cached before the prefetching starting.
  34. /// - `failedResources`: An array of resources that fail to be downloaded. It could because of being cancelled while
  35. /// downloading, encountered an error when downloading or the download not being started at all.
  36. /// - `completedResources`: An array of resources that are downloaded and cached successfully.
  37. public typealias PrefetcherProgressBlock =
  38. ((_ skippedResources: [Resource], _ failedResources: [Resource], _ completedResources: [Resource]) -> Void)
  39. /// Progress update block of prefetcher when initialized with a list of resources.
  40. ///
  41. /// - `skippedSources`: An array of sources that are already cached before the prefetching starting.
  42. /// - `failedSources`: An array of sources that fail to be fetched.
  43. /// - `completedResources`: An array of sources that are fetched and cached successfully.
  44. public typealias PrefetcherSourceProgressBlock =
  45. ((_ skippedSources: [Source], _ failedSources: [Source], _ completedSources: [Source]) -> Void)
  46. /// Completion block of prefetcher when initialized with a list of sources.
  47. ///
  48. /// - `skippedResources`: An array of resources that are already cached before the prefetching starting.
  49. /// - `failedResources`: An array of resources that fail to be downloaded. It could because of being cancelled while
  50. /// downloading, encountered an error when downloading or the download not being started at all.
  51. /// - `completedResources`: An array of resources that are downloaded and cached successfully.
  52. public typealias PrefetcherCompletionHandler =
  53. ((_ skippedResources: [Resource], _ failedResources: [Resource], _ completedResources: [Resource]) -> Void)
  54. /// Completion block of prefetcher when initialized with a list of sources.
  55. ///
  56. /// - `skippedSources`: An array of sources that are already cached before the prefetching starting.
  57. /// - `failedSources`: An array of sources that fail to be fetched.
  58. /// - `completedSources`: An array of sources that are fetched and cached successfully.
  59. public typealias PrefetcherSourceCompletionHandler =
  60. ((_ skippedSources: [Source], _ failedSources: [Source], _ completedSources: [Source]) -> Void)
  61. /// `ImagePrefetcher` represents a downloading manager for requesting many images via URLs, then caching them.
  62. /// This is useful when you know a list of image resources and want to download them before showing. It also works with
  63. /// some Cocoa prefetching mechanism like table view or collection view `prefetchDataSource`, to start image downloading
  64. /// and caching before they display on screen.
  65. public class ImagePrefetcher: CustomStringConvertible {
  66. public var description: String {
  67. return "\(Unmanaged.passUnretained(self).toOpaque())"
  68. }
  69. /// The maximum concurrent downloads to use when prefetching images. Default is 5.
  70. public var maxConcurrentDownloads = 5
  71. private let prefetchSources: [Source]
  72. private let optionsInfo: KingfisherParsedOptionsInfo
  73. private var progressBlock: PrefetcherProgressBlock?
  74. private var completionHandler: PrefetcherCompletionHandler?
  75. private var progressSourceBlock: PrefetcherSourceProgressBlock?
  76. private var completionSourceHandler: PrefetcherSourceCompletionHandler?
  77. private var tasks = [String: DownloadTask.WrappedTask]()
  78. private var pendingSources: ArraySlice<Source>
  79. private var skippedSources = [Source]()
  80. private var completedSources = [Source]()
  81. private var failedSources = [Source]()
  82. private var stopped = false
  83. // A manager used for prefetching. We will use the helper methods in manager.
  84. private let manager: KingfisherManager
  85. private let prefetchQueue = DispatchQueue(label: "com.onevcat.Kingfisher.ImagePrefetcher.prefetchQueue")
  86. private static let requestingQueue = DispatchQueue(label: "com.onevcat.Kingfisher.ImagePrefetcher.requestingQueue")
  87. private var finished: Bool {
  88. let totalFinished: Int = failedSources.count + skippedSources.count + completedSources.count
  89. return totalFinished == prefetchSources.count && tasks.isEmpty
  90. }
  91. /// Creates an image prefetcher with an array of URLs.
  92. ///
  93. /// The prefetcher should be initiated with a list of prefetching targets. The URLs list is immutable.
  94. /// After you get a valid `ImagePrefetcher` object, you call `start()` on it to begin the prefetching process.
  95. /// The images which are already cached will be skipped without downloading again.
  96. ///
  97. /// - Parameters:
  98. /// - urls: The URLs which should be prefetched.
  99. /// - options: Options could control some behaviors. See `KingfisherOptionsInfo` for more.
  100. /// - progressBlock: Called every time an resource is downloaded, skipped or cancelled.
  101. /// - completionHandler: Called when the whole prefetching process finished.
  102. ///
  103. /// - Note:
  104. /// By default, the `ImageDownloader.defaultDownloader` and `ImageCache.defaultCache` will be used as
  105. /// the downloader and cache target respectively. You can specify another downloader or cache by using
  106. /// a customized `KingfisherOptionsInfo`. Both the progress and completion block will be invoked in
  107. /// main thread. The `.callbackQueue` value in `optionsInfo` will be ignored in this method.
  108. public convenience init(
  109. urls: [URL],
  110. options: KingfisherOptionsInfo? = nil,
  111. progressBlock: PrefetcherProgressBlock? = nil,
  112. completionHandler: PrefetcherCompletionHandler? = nil)
  113. {
  114. let resources: [Resource] = urls.map { $0 }
  115. self.init(
  116. resources: resources,
  117. options: options,
  118. progressBlock: progressBlock,
  119. completionHandler: completionHandler)
  120. }
  121. /// Creates an image prefetcher with an array of URLs.
  122. ///
  123. /// The prefetcher should be initiated with a list of prefetching targets. The URLs list is immutable.
  124. /// After you get a valid `ImagePrefetcher` object, you call `start()` on it to begin the prefetching process.
  125. /// The images which are already cached will be skipped without downloading again.
  126. ///
  127. /// - Parameters:
  128. /// - urls: The URLs which should be prefetched.
  129. /// - options: Options could control some behaviors. See `KingfisherOptionsInfo` for more.
  130. /// - completionHandler: Called when the whole prefetching process finished.
  131. ///
  132. /// - Note:
  133. /// By default, the `ImageDownloader.defaultDownloader` and `ImageCache.defaultCache` will be used as
  134. /// the downloader and cache target respectively. You can specify another downloader or cache by using
  135. /// a customized `KingfisherOptionsInfo`. Both the progress and completion block will be invoked in
  136. /// main thread. The `.callbackQueue` value in `optionsInfo` will be ignored in this method.
  137. public convenience init(
  138. urls: [URL],
  139. options: KingfisherOptionsInfo? = nil,
  140. completionHandler: PrefetcherCompletionHandler? = nil)
  141. {
  142. let resources: [Resource] = urls.map { $0 }
  143. self.init(
  144. resources: resources,
  145. options: options,
  146. progressBlock: nil,
  147. completionHandler: completionHandler)
  148. }
  149. /// Creates an image prefetcher with an array of resources.
  150. ///
  151. /// - Parameters:
  152. /// - resources: The resources which should be prefetched. See `Resource` type for more.
  153. /// - options: Options could control some behaviors. See `KingfisherOptionsInfo` for more.
  154. /// - progressBlock: Called every time an resource is downloaded, skipped or cancelled.
  155. /// - completionHandler: Called when the whole prefetching process finished.
  156. ///
  157. /// - Note:
  158. /// By default, the `ImageDownloader.defaultDownloader` and `ImageCache.defaultCache` will be used as
  159. /// the downloader and cache target respectively. You can specify another downloader or cache by using
  160. /// a customized `KingfisherOptionsInfo`. Both the progress and completion block will be invoked in
  161. /// main thread. The `.callbackQueue` value in `optionsInfo` will be ignored in this method.
  162. public convenience init(
  163. resources: [Resource],
  164. options: KingfisherOptionsInfo? = nil,
  165. progressBlock: PrefetcherProgressBlock? = nil,
  166. completionHandler: PrefetcherCompletionHandler? = nil)
  167. {
  168. self.init(sources: resources.map { $0.convertToSource() }, options: options)
  169. self.progressBlock = progressBlock
  170. self.completionHandler = completionHandler
  171. }
  172. /// Creates an image prefetcher with an array of resources.
  173. ///
  174. /// - Parameters:
  175. /// - resources: The resources which should be prefetched. See `Resource` type for more.
  176. /// - options: Options could control some behaviors. See `KingfisherOptionsInfo` for more.
  177. /// - completionHandler: Called when the whole prefetching process finished.
  178. ///
  179. /// - Note:
  180. /// By default, the `ImageDownloader.defaultDownloader` and `ImageCache.defaultCache` will be used as
  181. /// the downloader and cache target respectively. You can specify another downloader or cache by using
  182. /// a customized `KingfisherOptionsInfo`. Both the progress and completion block will be invoked in
  183. /// main thread. The `.callbackQueue` value in `optionsInfo` will be ignored in this method.
  184. public convenience init(
  185. resources: [Resource],
  186. options: KingfisherOptionsInfo? = nil,
  187. completionHandler: PrefetcherCompletionHandler? = nil)
  188. {
  189. self.init(sources: resources.map { $0.convertToSource() }, options: options)
  190. self.completionHandler = completionHandler
  191. }
  192. /// Creates an image prefetcher with an array of sources.
  193. ///
  194. /// - Parameters:
  195. /// - sources: The sources which should be prefetched. See `Source` type for more.
  196. /// - options: Options could control some behaviors. See `KingfisherOptionsInfo` for more.
  197. /// - progressBlock: Called every time an source fetching successes, fails, is skipped.
  198. /// - completionHandler: Called when the whole prefetching process finished.
  199. ///
  200. /// - Note:
  201. /// By default, the `ImageDownloader.defaultDownloader` and `ImageCache.defaultCache` will be used as
  202. /// the downloader and cache target respectively. You can specify another downloader or cache by using
  203. /// a customized `KingfisherOptionsInfo`. Both the progress and completion block will be invoked in
  204. /// main thread. The `.callbackQueue` value in `optionsInfo` will be ignored in this method.
  205. public convenience init(sources: [Source],
  206. options: KingfisherOptionsInfo? = nil,
  207. progressBlock: PrefetcherSourceProgressBlock? = nil,
  208. completionHandler: PrefetcherSourceCompletionHandler? = nil)
  209. {
  210. self.init(sources: sources, options: options)
  211. self.progressSourceBlock = progressBlock
  212. self.completionSourceHandler = completionHandler
  213. }
  214. /// Creates an image prefetcher with an array of sources.
  215. ///
  216. /// - Parameters:
  217. /// - sources: The sources which should be prefetched. See `Source` type for more.
  218. /// - options: Options could control some behaviors. See `KingfisherOptionsInfo` for more.
  219. /// - completionHandler: Called when the whole prefetching process finished.
  220. ///
  221. /// - Note:
  222. /// By default, the `ImageDownloader.defaultDownloader` and `ImageCache.defaultCache` will be used as
  223. /// the downloader and cache target respectively. You can specify another downloader or cache by using
  224. /// a customized `KingfisherOptionsInfo`. Both the progress and completion block will be invoked in
  225. /// main thread. The `.callbackQueue` value in `optionsInfo` will be ignored in this method.
  226. public convenience init(sources: [Source],
  227. options: KingfisherOptionsInfo? = nil,
  228. completionHandler: PrefetcherSourceCompletionHandler? = nil)
  229. {
  230. self.init(sources: sources, options: options)
  231. self.completionSourceHandler = completionHandler
  232. }
  233. init(sources: [Source], options: KingfisherOptionsInfo?) {
  234. var options = KingfisherParsedOptionsInfo(options)
  235. prefetchSources = sources
  236. pendingSources = ArraySlice(sources)
  237. // We want all callbacks from our prefetch queue, so we should ignore the callback queue in options.
  238. // Add our own callback dispatch queue to make sure all internal callbacks are
  239. // coming back in our expected queue.
  240. options.callbackQueue = .dispatch(prefetchQueue)
  241. optionsInfo = options
  242. let cache = optionsInfo.targetCache ?? .default
  243. let downloader = optionsInfo.downloader ?? .default
  244. manager = KingfisherManager(downloader: downloader, cache: cache)
  245. }
  246. /// Starts to download the resources and cache them. This can be useful for background downloading
  247. /// of assets that are required for later use in an app. This code will not try and update any UI
  248. /// with the results of the process.
  249. public func start() {
  250. prefetchQueue.async {
  251. guard !self.stopped else {
  252. assertionFailure("You can not restart the same prefetcher. Try to create a new prefetcher.")
  253. self.handleComplete()
  254. return
  255. }
  256. guard self.maxConcurrentDownloads > 0 else {
  257. assertionFailure("There should be concurrent downloads value should be at least 1.")
  258. self.handleComplete()
  259. return
  260. }
  261. // Empty case.
  262. guard self.prefetchSources.count > 0 else {
  263. self.handleComplete()
  264. return
  265. }
  266. let initialConcurrentDownloads = min(self.prefetchSources.count, self.maxConcurrentDownloads)
  267. for _ in 0 ..< initialConcurrentDownloads {
  268. if let resource = self.pendingSources.popFirst() {
  269. self.startPrefetching(resource)
  270. }
  271. }
  272. }
  273. }
  274. /// Stops current downloading progress, and cancel any future prefetching activity that might be occuring.
  275. public func stop() {
  276. prefetchQueue.async {
  277. if self.finished { return }
  278. self.stopped = true
  279. self.tasks.values.forEach { $0.cancel() }
  280. }
  281. }
  282. private func downloadAndCache(_ source: Source) {
  283. let downloadTaskCompletionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void) = { result in
  284. self.tasks.removeValue(forKey: source.cacheKey)
  285. do {
  286. let _ = try result.get()
  287. self.completedSources.append(source)
  288. } catch {
  289. self.failedSources.append(source)
  290. }
  291. self.reportProgress()
  292. if self.stopped {
  293. if self.tasks.isEmpty {
  294. self.failedSources.append(contentsOf: self.pendingSources)
  295. self.handleComplete()
  296. }
  297. } else {
  298. self.reportCompletionOrStartNext()
  299. }
  300. }
  301. var downloadTask: DownloadTask.WrappedTask?
  302. ImagePrefetcher.requestingQueue.sync {
  303. let context = RetrievingContext(
  304. options: optionsInfo, originalSource: source
  305. )
  306. downloadTask = manager.loadAndCacheImage(
  307. source: source,
  308. context: context,
  309. completionHandler: downloadTaskCompletionHandler)
  310. }
  311. if let downloadTask = downloadTask {
  312. tasks[source.cacheKey] = downloadTask
  313. }
  314. }
  315. private func append(cached source: Source) {
  316. skippedSources.append(source)
  317. reportProgress()
  318. reportCompletionOrStartNext()
  319. }
  320. private func startPrefetching(_ source: Source)
  321. {
  322. if optionsInfo.forceRefresh {
  323. downloadAndCache(source)
  324. return
  325. }
  326. let cacheType = manager.cache.imageCachedType(
  327. forKey: source.cacheKey,
  328. processorIdentifier: optionsInfo.processor.identifier)
  329. switch cacheType {
  330. case .memory:
  331. append(cached: source)
  332. case .disk:
  333. if optionsInfo.alsoPrefetchToMemory {
  334. let context = RetrievingContext(options: optionsInfo, originalSource: source)
  335. _ = manager.retrieveImageFromCache(
  336. source: source,
  337. context: context)
  338. {
  339. _ in
  340. self.append(cached: source)
  341. }
  342. } else {
  343. append(cached: source)
  344. }
  345. case .none:
  346. downloadAndCache(source)
  347. }
  348. }
  349. private func reportProgress() {
  350. if progressBlock == nil && progressSourceBlock == nil {
  351. return
  352. }
  353. let skipped = self.skippedSources
  354. let failed = self.failedSources
  355. let completed = self.completedSources
  356. CallbackQueue.mainCurrentOrAsync.execute {
  357. self.progressSourceBlock?(skipped, failed, completed)
  358. self.progressBlock?(
  359. skipped.compactMap { $0.asResource },
  360. failed.compactMap { $0.asResource },
  361. completed.compactMap { $0.asResource }
  362. )
  363. }
  364. }
  365. private func reportCompletionOrStartNext() {
  366. if let resource = self.pendingSources.popFirst() {
  367. // Loose call stack for huge ammount of sources.
  368. prefetchQueue.async { self.startPrefetching(resource) }
  369. } else {
  370. guard allFinished else { return }
  371. self.handleComplete()
  372. }
  373. }
  374. var allFinished: Bool {
  375. return skippedSources.count + failedSources.count + completedSources.count == prefetchSources.count
  376. }
  377. private func handleComplete() {
  378. if completionHandler == nil && completionSourceHandler == nil {
  379. return
  380. }
  381. // The completion handler should be called on the main thread
  382. CallbackQueue.mainCurrentOrAsync.execute {
  383. self.completionSourceHandler?(self.skippedSources, self.failedSources, self.completedSources)
  384. self.completionHandler?(
  385. self.skippedSources.compactMap { $0.asResource },
  386. self.failedSources.compactMap { $0.asResource },
  387. self.completedSources.compactMap { $0.asResource }
  388. )
  389. self.completionHandler = nil
  390. self.progressBlock = nil
  391. }
  392. }
  393. }