KingfisherManager.swift 35 KB

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