KingfisherManager.swift 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. //
  2. // KingfisherManager.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/4/6.
  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. import Foundation
  27. /// The downloading progress block type.
  28. /// The parameter value is the `receivedSize` of current response.
  29. /// The second parameter is the total expected data length from response's "Content-Length" header.
  30. /// If the expected length is not available, this block will not be called.
  31. public typealias DownloadProgressBlock = ((_ receivedSize: Int64, _ totalSize: Int64) -> Void)
  32. /// Represents the result of a Kingfisher retrieving image task.
  33. public struct RetrieveImageResult {
  34. /// Gets the image object of this result.
  35. public let image: KFCrossPlatformImage
  36. /// Gets the cache source of the image. It indicates from which layer of cache this image is retrieved.
  37. /// If the image is just downloaded from network, `.none` will be returned.
  38. public let cacheType: CacheType
  39. /// The `Source` which this result is related to. This indicated where the `image` of `self` is referring.
  40. public let source: Source
  41. /// The original `Source` from which the retrieve task begins. It can be different from the `source` property.
  42. /// When an alternative source loading happened, the `source` will be the replacing loading target, while the
  43. /// `originalSource` will be kept as the initial `source` which issued the image loading process.
  44. public let originalSource: Source
  45. }
  46. /// A struct that stores some related information of an `KingfisherError`. It provides some context information for
  47. /// a pure error so you can identify the error easier.
  48. public struct PropagationError {
  49. /// The `Source` to which current `error` is bound.
  50. public let source: Source
  51. /// The actual error happens in framework.
  52. public let error: KingfisherError
  53. }
  54. /// The downloading task updated block type. The parameter `newTask` is the updated new task of image setting process.
  55. /// It is a `nil` if the image loading does not require an image downloading process. If an image downloading is issued,
  56. /// this value will contain the actual `DownloadTask` for you to keep and cancel it later if you need.
  57. public typealias DownloadTaskUpdatedBlock = ((_ newTask: DownloadTask?) -> Void)
  58. /// Main manager class of Kingfisher. It connects Kingfisher downloader and cache,
  59. /// to provide a set of convenience methods to use Kingfisher for tasks.
  60. /// You can use this class to retrieve an image via a specified URL from web or cache.
  61. public class KingfisherManager {
  62. /// Represents a shared manager used across Kingfisher.
  63. /// Use this instance for getting or storing images with Kingfisher.
  64. public static let shared = KingfisherManager()
  65. // Mark: Public Properties
  66. /// The `ImageCache` used by this manager. It is `ImageCache.default` by default.
  67. /// If a cache is specified in `KingfisherManager.defaultOptions`, the value in `defaultOptions` will be
  68. /// used instead.
  69. public var cache: ImageCache
  70. /// The `ImageDownloader` used by this manager. It is `ImageDownloader.default` by default.
  71. /// If a downloader is specified in `KingfisherManager.defaultOptions`, the value in `defaultOptions` will be
  72. /// used instead.
  73. public var downloader: ImageDownloader
  74. /// Default options used by the manager. This option will be used in
  75. /// Kingfisher manager related methods, as well as all view extension methods.
  76. /// You can also passing other options for each image task by sending an `options` parameter
  77. /// to Kingfisher's APIs. The per image options will overwrite the default ones,
  78. /// if the option exists in both.
  79. public var defaultOptions = KingfisherOptionsInfo.empty
  80. // Use `defaultOptions` to overwrite the `downloader` and `cache`.
  81. private var currentDefaultOptions: KingfisherOptionsInfo {
  82. return [.downloader(downloader), .targetCache(cache)] + defaultOptions
  83. }
  84. private let processingQueue: CallbackQueue
  85. private convenience init() {
  86. self.init(downloader: .default, cache: .default)
  87. }
  88. /// Creates an image setting manager with specified downloader and cache.
  89. ///
  90. /// - Parameters:
  91. /// - downloader: The image downloader used to download images.
  92. /// - cache: The image cache which stores memory and disk images.
  93. public init(downloader: ImageDownloader, cache: ImageCache) {
  94. self.downloader = downloader
  95. self.cache = cache
  96. let processQueueName = "com.onevcat.Kingfisher.KingfisherManager.processQueue.\(UUID().uuidString)"
  97. processingQueue = .dispatch(DispatchQueue(label: processQueueName))
  98. }
  99. // MARK: - Getting Images
  100. /// Gets an image from a given resource.
  101. /// - Parameters:
  102. /// - resource: The `Resource` object defines data information like key or URL.
  103. /// - options: Options to use when creating the image.
  104. /// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
  105. /// `expectedContentLength`, this block will not be called. `progressBlock` is always called in
  106. /// main queue.
  107. /// - downloadTaskUpdated: Called when a new image downloading task is created for current image retrieving. This
  108. /// usually happens when an alternative source is used to replace the original (failed)
  109. /// task. You can update your reference of `DownloadTask` if you want to manually `cancel`
  110. /// the new task.
  111. /// - completionHandler: Called when the image retrieved and set finished. This completion handler will be invoked
  112. /// from the `options.callbackQueue`. If not specified, the main queue will be used.
  113. /// - Returns: A task represents the image downloading. If there is a download task starts for `.network` resource,
  114. /// the started `DownloadTask` is returned. Otherwise, `nil` is returned.
  115. ///
  116. /// - Note:
  117. /// This method will first check whether the requested `resource` is already in cache or not. If cached,
  118. /// it returns `nil` and invoke the `completionHandler` after the cached image retrieved. Otherwise, it
  119. /// will download the `resource`, store it in cache, then call `completionHandler`.
  120. @discardableResult
  121. public func retrieveImage(
  122. with resource: Resource,
  123. options: KingfisherOptionsInfo? = nil,
  124. progressBlock: DownloadProgressBlock? = nil,
  125. downloadTaskUpdated: DownloadTaskUpdatedBlock? = nil,
  126. completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
  127. {
  128. return retrieveImage(
  129. with: resource.convertToSource(),
  130. options: options,
  131. progressBlock: progressBlock,
  132. downloadTaskUpdated: downloadTaskUpdated,
  133. completionHandler: completionHandler
  134. )
  135. }
  136. /// Gets an image from a given resource.
  137. ///
  138. /// - Parameters:
  139. /// - source: The `Source` object defines data information from network or a data provider.
  140. /// - options: Options to use when creating the image.
  141. /// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
  142. /// `expectedContentLength`, this block will not be called. `progressBlock` is always called in
  143. /// main queue.
  144. /// - downloadTaskUpdated: Called when a new image downloading task is created for current image retrieving. This
  145. /// usually happens when an alternative source is used to replace the original (failed)
  146. /// task. You can update your reference of `DownloadTask` if you want to manually `cancel`
  147. /// the new task.
  148. /// - completionHandler: Called when the image retrieved and set finished. This completion handler will be invoked
  149. /// from the `options.callbackQueue`. If not specified, the main queue will be used.
  150. /// - Returns: A task represents the image downloading. If there is a download task starts for `.network` resource,
  151. /// the started `DownloadTask` is returned. Otherwise, `nil` is returned.
  152. ///
  153. /// - Note:
  154. /// This method will first check whether the requested `source` is already in cache or not. If cached,
  155. /// it returns `nil` and invoke the `completionHandler` after the cached image retrieved. Otherwise, it
  156. /// will try to load the `source`, store it in cache, then call `completionHandler`.
  157. ///
  158. public func retrieveImage(
  159. with source: Source,
  160. options: KingfisherOptionsInfo? = nil,
  161. progressBlock: DownloadProgressBlock? = nil,
  162. downloadTaskUpdated: DownloadTaskUpdatedBlock? = nil,
  163. completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
  164. {
  165. let options = currentDefaultOptions + (options ?? .empty)
  166. var info = KingfisherParsedOptionsInfo(options)
  167. if let block = progressBlock {
  168. info.onDataReceived = (info.onDataReceived ?? []) + [ImageLoadingProgressSideEffect(block)]
  169. }
  170. return retrieveImage(
  171. with: source,
  172. options: info,
  173. downloadTaskUpdated: downloadTaskUpdated,
  174. completionHandler: completionHandler)
  175. }
  176. func retrieveImage(
  177. with source: Source,
  178. options: KingfisherParsedOptionsInfo,
  179. downloadTaskUpdated: DownloadTaskUpdatedBlock? = nil,
  180. completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
  181. {
  182. var retrievingContext = RetrievingContext(options: options, originalSource: source)
  183. var retryContext: RetryContext?
  184. func startNewRetrieveTask(
  185. with source: Source,
  186. downloadTaskUpdated: DownloadTaskUpdatedBlock?
  187. ) {
  188. let newTask = self.retrieveImage(with: source, context: retrievingContext) { result in
  189. handler(currentSource: source, result: result)
  190. }
  191. downloadTaskUpdated?(newTask)
  192. }
  193. func failCurrentSource(_ source: Source, with error: KingfisherError) {
  194. // Skip alternative sources if the user cancelled it.
  195. guard !error.isTaskCancelled else {
  196. completionHandler?(.failure(error))
  197. return
  198. }
  199. if let nextSource = retrievingContext.popAlternativeSource() {
  200. startNewRetrieveTask(with: nextSource, downloadTaskUpdated: downloadTaskUpdated)
  201. } else {
  202. // No other alternative source. Finish with error.
  203. if retrievingContext.propagationErrors.isEmpty {
  204. completionHandler?(.failure(error))
  205. } else {
  206. retrievingContext.appendError(error, to: source)
  207. let finalError = KingfisherError.imageSettingError(
  208. reason: .alternativeSourcesExhausted(retrievingContext.propagationErrors)
  209. )
  210. completionHandler?(.failure(finalError))
  211. }
  212. }
  213. }
  214. func handler(currentSource: Source, result: (Result<RetrieveImageResult, KingfisherError>)) -> Void {
  215. switch result {
  216. case .success:
  217. completionHandler?(result)
  218. case .failure(let error):
  219. if let retryStrategy = options.retryStrategy {
  220. let context = retryContext?.increaseRetryCount() ?? RetryContext(source: source, error: error)
  221. retryContext = context
  222. retryStrategy.retry(context: context) { decision in
  223. switch decision {
  224. case .retry(let userInfo):
  225. retryContext?.userInfo = userInfo
  226. startNewRetrieveTask(with: source, downloadTaskUpdated: downloadTaskUpdated)
  227. case .stop:
  228. failCurrentSource(currentSource, with: error)
  229. }
  230. }
  231. } else {
  232. // Skip alternative sources if the user cancelled it.
  233. guard !error.isTaskCancelled else {
  234. completionHandler?(.failure(error))
  235. return
  236. }
  237. if let nextSource = retrievingContext.popAlternativeSource() {
  238. retrievingContext.appendError(error, to: currentSource)
  239. startNewRetrieveTask(with: nextSource, downloadTaskUpdated: downloadTaskUpdated)
  240. } else {
  241. // No other alternative source. Finish with error.
  242. if retrievingContext.propagationErrors.isEmpty {
  243. completionHandler?(.failure(error))
  244. } else {
  245. retrievingContext.appendError(error, to: currentSource)
  246. let finalError = KingfisherError.imageSettingError(
  247. reason: .alternativeSourcesExhausted(retrievingContext.propagationErrors)
  248. )
  249. completionHandler?(.failure(finalError))
  250. }
  251. }
  252. }
  253. }
  254. }
  255. return retrieveImage(
  256. with: source,
  257. context: retrievingContext)
  258. {
  259. result in
  260. handler(currentSource: source, result: result)
  261. }
  262. }
  263. private func retrieveImage(
  264. with source: Source,
  265. context: RetrievingContext,
  266. completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
  267. {
  268. let options = context.options
  269. if options.forceRefresh {
  270. return loadAndCacheImage(
  271. source: source,
  272. context: context,
  273. completionHandler: completionHandler)?.value
  274. } else {
  275. let loadedFromCache = retrieveImageFromCache(
  276. source: source,
  277. context: context,
  278. completionHandler: completionHandler)
  279. if loadedFromCache {
  280. return nil
  281. }
  282. if options.onlyFromCache {
  283. let error = KingfisherError.cacheError(reason: .imageNotExisting(key: source.cacheKey))
  284. completionHandler?(.failure(error))
  285. return nil
  286. }
  287. return loadAndCacheImage(
  288. source: source,
  289. context: context,
  290. completionHandler: completionHandler)?.value
  291. }
  292. }
  293. func provideImage(
  294. provider: ImageDataProvider,
  295. options: KingfisherParsedOptionsInfo,
  296. completionHandler: ((Result<ImageLoadingResult, KingfisherError>) -> Void)?)
  297. {
  298. guard let completionHandler = completionHandler else { return }
  299. provider.data { result in
  300. switch result {
  301. case .success(let data):
  302. (options.processingQueue ?? self.processingQueue).execute {
  303. let processor = options.processor
  304. let processingItem = ImageProcessItem.data(data)
  305. guard let image = processor.process(item: processingItem, options: options) else {
  306. options.callbackQueue.execute {
  307. let error = KingfisherError.processorError(
  308. reason: .processingFailed(processor: processor, item: processingItem))
  309. completionHandler(.failure(error))
  310. }
  311. return
  312. }
  313. let finalImage = options.imageModifier?.modify(image) ?? image
  314. options.callbackQueue.execute {
  315. let result = ImageLoadingResult(image: finalImage, url: nil, originalData: data)
  316. completionHandler(.success(result))
  317. }
  318. }
  319. case .failure(let error):
  320. options.callbackQueue.execute {
  321. let error = KingfisherError.imageSettingError(
  322. reason: .dataProviderError(provider: provider, error: error))
  323. completionHandler(.failure(error))
  324. }
  325. }
  326. }
  327. }
  328. private func cacheImage(
  329. source: Source,
  330. options: KingfisherParsedOptionsInfo,
  331. context: RetrievingContext,
  332. result: Result<ImageLoadingResult, KingfisherError>,
  333. completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?
  334. )
  335. {
  336. switch result {
  337. case .success(let value):
  338. let needToCacheOriginalImage = options.cacheOriginalImage &&
  339. options.processor != DefaultImageProcessor.default
  340. let coordinator = CacheCallbackCoordinator(
  341. shouldWaitForCache: options.waitForCache, shouldCacheOriginal: needToCacheOriginalImage)
  342. // Add image to cache.
  343. let targetCache = options.targetCache ?? self.cache
  344. targetCache.store(
  345. value.image,
  346. original: value.originalData,
  347. forKey: source.cacheKey,
  348. options: options,
  349. toDisk: !options.cacheMemoryOnly)
  350. {
  351. _ in
  352. coordinator.apply(.cachingImage) {
  353. let result = RetrieveImageResult(
  354. image: value.image,
  355. cacheType: .none,
  356. source: source,
  357. originalSource: context.originalSource
  358. )
  359. completionHandler?(.success(result))
  360. }
  361. }
  362. // Add original image to cache if necessary.
  363. if needToCacheOriginalImage {
  364. let originalCache = options.originalCache ?? targetCache
  365. originalCache.storeToDisk(
  366. value.originalData,
  367. forKey: source.cacheKey,
  368. processorIdentifier: DefaultImageProcessor.default.identifier,
  369. expiration: options.diskCacheExpiration)
  370. {
  371. _ in
  372. coordinator.apply(.cachingOriginalImage) {
  373. let result = RetrieveImageResult(
  374. image: value.image,
  375. cacheType: .none,
  376. source: source,
  377. originalSource: context.originalSource
  378. )
  379. completionHandler?(.success(result))
  380. }
  381. }
  382. }
  383. coordinator.apply(.cacheInitiated) {
  384. let result = RetrieveImageResult(
  385. image: value.image,
  386. cacheType: .none,
  387. source: source,
  388. originalSource: context.originalSource
  389. )
  390. completionHandler?(.success(result))
  391. }
  392. case .failure(let error):
  393. completionHandler?(.failure(error))
  394. }
  395. }
  396. @discardableResult
  397. func loadAndCacheImage(
  398. source: Source,
  399. context: RetrievingContext,
  400. completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask.WrappedTask?
  401. {
  402. let options = context.options
  403. func _cacheImage(_ result: Result<ImageLoadingResult, KingfisherError>) {
  404. cacheImage(
  405. source: source,
  406. options: options,
  407. context: context,
  408. result: result,
  409. completionHandler: completionHandler
  410. )
  411. }
  412. switch source {
  413. case .network(let resource):
  414. let downloader = options.downloader ?? self.downloader
  415. let task = downloader.downloadImage(
  416. with: resource.downloadURL, options: options, completionHandler: _cacheImage
  417. )
  418. // The code below is neat, but it fails the Swift 5.2 compiler with a runtime crash when
  419. // `BUILD_LIBRARY_FOR_DISTRIBUTION` is turned on. I believe it is a bug in the compiler.
  420. // Let's fallback to a traditional style before it can be fixed in Swift.
  421. //
  422. // https://github.com/onevcat/Kingfisher/issues/1436
  423. //
  424. // return task.map(DownloadTask.WrappedTask.download)
  425. if let task = task {
  426. return .download(task)
  427. } else {
  428. return nil
  429. }
  430. case .provider(let provider):
  431. provideImage(provider: provider, options: options, completionHandler: _cacheImage)
  432. return .dataProviding
  433. }
  434. }
  435. /// Retrieves image from memory or disk cache.
  436. ///
  437. /// - Parameters:
  438. /// - source: The target source from which to get image.
  439. /// - key: The key to use when caching the image.
  440. /// - url: Image request URL. This is not used when retrieving image from cache. It is just used for
  441. /// `RetrieveImageResult` callback compatibility.
  442. /// - options: Options on how to get the image from image cache.
  443. /// - completionHandler: Called when the image retrieving finishes, either with succeeded
  444. /// `RetrieveImageResult` or an error.
  445. /// - Returns: `true` if the requested image or the original image before being processed is existing in cache.
  446. /// Otherwise, this method returns `false`.
  447. ///
  448. /// - Note:
  449. /// The image retrieving could happen in either memory cache or disk cache. The `.processor` option in
  450. /// `options` will be considered when searching in the cache. If no processed image is found, Kingfisher
  451. /// will try to check whether an original version of that image is existing or not. If there is already an
  452. /// original, Kingfisher retrieves it from cache and processes it. Then, the processed image will be store
  453. /// back to cache for later use.
  454. func retrieveImageFromCache(
  455. source: Source,
  456. context: RetrievingContext,
  457. completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> Bool
  458. {
  459. let options = context.options
  460. // 1. Check whether the image was already in target cache. If so, just get it.
  461. let targetCache = options.targetCache ?? cache
  462. let key = source.cacheKey
  463. let targetImageCached = targetCache.imageCachedType(
  464. forKey: key, processorIdentifier: options.processor.identifier)
  465. let validCache = targetImageCached.cached &&
  466. (options.fromMemoryCacheOrRefresh == false || targetImageCached == .memory)
  467. if validCache {
  468. targetCache.retrieveImage(forKey: key, options: options) { result in
  469. guard let completionHandler = completionHandler else { return }
  470. options.callbackQueue.execute {
  471. result.match(
  472. onSuccess: { cacheResult in
  473. let value: Result<RetrieveImageResult, KingfisherError>
  474. if let image = cacheResult.image {
  475. value = result.map {
  476. RetrieveImageResult(
  477. image: image,
  478. cacheType: $0.cacheType,
  479. source: source,
  480. originalSource: context.originalSource
  481. )
  482. }
  483. } else {
  484. value = .failure(KingfisherError.cacheError(reason: .imageNotExisting(key: key)))
  485. }
  486. completionHandler(value)
  487. },
  488. onFailure: { _ in
  489. completionHandler(.failure(KingfisherError.cacheError(reason: .imageNotExisting(key: key))))
  490. }
  491. )
  492. }
  493. }
  494. return true
  495. }
  496. // 2. Check whether the original image exists. If so, get it, process it, save to storage and return.
  497. let originalCache = options.originalCache ?? targetCache
  498. // No need to store the same file in the same cache again.
  499. if originalCache === targetCache && options.processor == DefaultImageProcessor.default {
  500. return false
  501. }
  502. // Check whether the unprocessed image existing or not.
  503. let originalImageCacheType = originalCache.imageCachedType(
  504. forKey: key, processorIdentifier: DefaultImageProcessor.default.identifier)
  505. let canAcceptDiskCache = !options.fromMemoryCacheOrRefresh
  506. let canUseOriginalImageCache =
  507. (canAcceptDiskCache && originalImageCacheType.cached) ||
  508. (!canAcceptDiskCache && originalImageCacheType == .memory)
  509. if canUseOriginalImageCache {
  510. // Now we are ready to get found the original image from cache. We need the unprocessed image, so remove
  511. // any processor from options first.
  512. var optionsWithoutProcessor = options
  513. optionsWithoutProcessor.processor = DefaultImageProcessor.default
  514. originalCache.retrieveImage(forKey: key, options: optionsWithoutProcessor) { result in
  515. result.match(
  516. onSuccess: { cacheResult in
  517. guard let image = cacheResult.image else {
  518. assertionFailure("The image (under key: \(key) should be existing in the original cache.")
  519. return
  520. }
  521. let processor = options.processor
  522. (options.processingQueue ?? self.processingQueue).execute {
  523. let item = ImageProcessItem.image(image)
  524. guard let processedImage = processor.process(item: item, options: options) else {
  525. let error = KingfisherError.processorError(
  526. reason: .processingFailed(processor: processor, item: item))
  527. options.callbackQueue.execute { completionHandler?(.failure(error)) }
  528. return
  529. }
  530. var cacheOptions = options
  531. cacheOptions.callbackQueue = .untouch
  532. let coordinator = CacheCallbackCoordinator(
  533. shouldWaitForCache: options.waitForCache, shouldCacheOriginal: false)
  534. targetCache.store(
  535. processedImage,
  536. forKey: key,
  537. options: cacheOptions,
  538. toDisk: !options.cacheMemoryOnly)
  539. {
  540. _ in
  541. coordinator.apply(.cachingImage) {
  542. let value = RetrieveImageResult(
  543. image: processedImage,
  544. cacheType: .none,
  545. source: source,
  546. originalSource: context.originalSource
  547. )
  548. options.callbackQueue.execute { completionHandler?(.success(value)) }
  549. }
  550. }
  551. coordinator.apply(.cacheInitiated) {
  552. let value = RetrieveImageResult(
  553. image: processedImage,
  554. cacheType: .none,
  555. source: source,
  556. originalSource: context.originalSource
  557. )
  558. options.callbackQueue.execute { completionHandler?(.success(value)) }
  559. }
  560. }
  561. },
  562. onFailure: { _ in
  563. // This should not happen actually, since we already confirmed `originalImageCached` is `true`.
  564. // Just in case...
  565. options.callbackQueue.execute {
  566. completionHandler?(
  567. .failure(KingfisherError.cacheError(reason: .imageNotExisting(key: key)))
  568. )
  569. }
  570. }
  571. )
  572. }
  573. return true
  574. }
  575. return false
  576. }
  577. }
  578. class RetrievingContext {
  579. var options: KingfisherParsedOptionsInfo
  580. let originalSource: Source
  581. var propagationErrors: [PropagationError] = []
  582. init(options: KingfisherParsedOptionsInfo, originalSource: Source) {
  583. self.originalSource = originalSource
  584. self.options = options
  585. }
  586. func popAlternativeSource() -> Source? {
  587. guard var alternativeSources = options.alternativeSources, !alternativeSources.isEmpty else {
  588. return nil
  589. }
  590. let nextSource = alternativeSources.removeFirst()
  591. options.alternativeSources = alternativeSources
  592. return nextSource
  593. }
  594. @discardableResult
  595. func appendError(_ error: KingfisherError, to source: Source) -> [PropagationError] {
  596. let item = PropagationError(source: source, error: error)
  597. propagationErrors.append(item)
  598. return propagationErrors
  599. }
  600. }
  601. class CacheCallbackCoordinator {
  602. enum State {
  603. case idle
  604. case imageCached
  605. case originalImageCached
  606. case done
  607. }
  608. enum Action {
  609. case cacheInitiated
  610. case cachingImage
  611. case cachingOriginalImage
  612. }
  613. private let shouldWaitForCache: Bool
  614. private let shouldCacheOriginal: Bool
  615. private let stateQueue: DispatchQueue
  616. private var threadSafeState: State = .idle
  617. private (set) var state: State {
  618. set { stateQueue.sync { threadSafeState = newValue } }
  619. get { stateQueue.sync { threadSafeState } }
  620. }
  621. init(shouldWaitForCache: Bool, shouldCacheOriginal: Bool) {
  622. self.shouldWaitForCache = shouldWaitForCache
  623. self.shouldCacheOriginal = shouldCacheOriginal
  624. let stateQueueName = "com.onevcat.Kingfisher.CacheCallbackCoordinator.stateQueue.\(UUID().uuidString)"
  625. self.stateQueue = DispatchQueue(label: stateQueueName)
  626. }
  627. func apply(_ action: Action, trigger: () -> Void) {
  628. switch (state, action) {
  629. case (.done, _):
  630. break
  631. // From .idle
  632. case (.idle, .cacheInitiated):
  633. if !shouldWaitForCache {
  634. state = .done
  635. trigger()
  636. }
  637. case (.idle, .cachingImage):
  638. if shouldCacheOriginal {
  639. state = .imageCached
  640. } else {
  641. state = .done
  642. trigger()
  643. }
  644. case (.idle, .cachingOriginalImage):
  645. state = .originalImageCached
  646. // From .imageCached
  647. case (.imageCached, .cachingOriginalImage):
  648. state = .done
  649. trigger()
  650. // From .originalImageCached
  651. case (.originalImageCached, .cachingImage):
  652. state = .done
  653. trigger()
  654. default:
  655. assertionFailure("This case should not happen in CacheCallbackCoordinator: \(state) - \(action)")
  656. }
  657. }
  658. }