-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathKingfisherManager.swift
972 lines (873 loc) · 42.5 KB
/
KingfisherManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
//
// KingfisherManager.swift
// Kingfisher
//
// Created by Wei Wang on 15/4/6.
//
// Copyright (c) 2019 Wei Wang <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
#if os(macOS)
import AppKit
#else
import UIKit
#endif
/// Represents the type for a downloading progress block.
///
/// This block type is used to monitor the progress of data being downloaded. It takes two parameters:
///
/// 1. `receivedSize`: The size of the data received in the current response.
/// 2. `expectedSize`: The total expected data length from the response's "Content-Length" header. If the expected
/// length is not available, this block will not be called.
///
/// You can use this progress block to track the download progress and update user interfaces or perform additional
/// actions based on the progress.
///
/// - Parameters:
/// - receivedSize: The size of the data received.
/// - expectedSize: The expected total data length from the "Content-Length" header.
public typealias DownloadProgressBlock = ((_ receivedSize: Int64, _ totalSize: Int64) -> Void)
/// Represents the result of a Kingfisher image retrieval task.
///
/// This type encapsulates the outcome of an image retrieval operation performed by Kingfisher.
/// It holds a successful result with the retrieved image.
public struct RetrieveImageResult: Sendable {
/// Retrieves the image object from this result.
public let image: KFCrossPlatformImage
/// Retrieves the cache source of the image, indicating from which cache layer it was retrieved.
///
/// If the image was freshly downloaded from the network and not retrieved from any cache, `.none` will be returned.
/// Otherwise, either ``CacheType/memory`` or ``CacheType/disk`` will be returned, allowing you to determine whether
/// the image was retrieved from memory or disk cache.
public let cacheType: CacheType
/// The ``Source`` to which this result is related. This indicates where the `image` referenced by `self` is located.
public let source: Source
/// The original ``Source`` from which the retrieval task begins. It may differ from the ``source`` property.
/// When an alternative source loading occurs, the ``source`` will represent the replacement loading target, while the
/// ``originalSource`` will retain the initial ``source`` that initiated the image loading process.
public let originalSource: Source
/// Retrieves the data associated with this result.
///
/// When this result is obtained from a network download (when `cacheType == .none`), calling this method returns
/// the downloaded data. If the result is from the cache, it serializes the image using the specified cache
/// serializer from the loading options and returns the result.
///
/// - Note: Retrieving this data can be a time-consuming operation, so it is advisable to store it if you need to
/// use it multiple times and avoid frequent calls to this method.
public let data: @Sendable () -> Data?
}
/// A structure that stores related information about a ``KingfisherError``. It provides contextual information
/// to facilitate the identification of the error.
public struct PropagationError: Sendable {
/// The ``Source`` to which current `error` is bound.
public let source: Source
/// The actual error happens in framework.
public let error: KingfisherError
}
/// The block type used for handling updates during the downloading task.
///
/// The `newTask` parameter represents the updated task for the image loading process. It is `nil` if the image loading
/// doesn't involve a downloading process. When an image download is initiated, this value will contain the actual
/// ``DownloadTask`` instance, allowing you to retain it or cancel it later if necessary.
public typealias DownloadTaskUpdatedBlock = (@Sendable (_ newTask: DownloadTask?) -> Void)
/// The main manager class of Kingfisher. It connects the Kingfisher downloader and cache to offer a set of convenient
/// methods for working with Kingfisher tasks.
///
/// You can utilize this class to retrieve an image via a specified URL from the web or cache.
public class KingfisherManager: @unchecked Sendable {
private let propertyQueue = DispatchQueue(label: "com.onevcat.Kingfisher.KingfisherManagerPropertyQueue")
/// Represents a shared manager used across Kingfisher.
/// Use this instance for getting or storing images with Kingfisher.
public static let shared = KingfisherManager()
// Mark: Public Properties
private var _cache: ImageCache
/// The ``ImageCache`` utilized by this manager, which defaults to ``ImageCache/default``.
///
/// If a cache is specified in ``KingfisherManager/defaultOptions`` or ``KingfisherOptionsInfoItem/targetCache(_:)``,
/// those specified values will take precedence when Kingfisher attempts to retrieve or store images in the cache.
public var cache: ImageCache {
get { propertyQueue.sync { _cache } }
set { propertyQueue.sync { _cache = newValue } }
}
private var _downloader: ImageDownloader
/// The ``ImageDownloader`` utilized by this manager, which defaults to ``ImageDownloader/default``.
///
/// If a downloader is specified in ``KingfisherManager/defaultOptions`` or ``KingfisherOptionsInfoItem/downloader(_:)``,
/// those specified values will take precedence when Kingfisher attempts to download the image data from a remote
/// server.
public var downloader: ImageDownloader {
get { propertyQueue.sync { _downloader } }
set { propertyQueue.sync { _downloader = newValue } }
}
/// The default options used by the ``KingfisherManager`` instance.
///
/// These options are utilized in Kingfisher manager-related methods, as well as all view extension methods.
/// You can also pass additional options for each image task by providing an `options` parameter to Kingfisher's APIs.
///
/// Per-image options will override the default ones if there is a conflict.
public var defaultOptions = KingfisherOptionsInfo.empty
// Use `defaultOptions` to overwrite the `downloader` and `cache`.
var currentDefaultOptions: KingfisherOptionsInfo {
return [.downloader(downloader), .targetCache(cache)] + defaultOptions
}
private let processingQueue: CallbackQueue
private convenience init() {
self.init(downloader: .default, cache: .default)
}
/// Creates an image setting manager with the specified downloader and cache.
///
/// - Parameters:
/// - downloader: The image downloader used for image downloads.
/// - cache: The image cache that stores images in memory and on disk.
///
public init(downloader: ImageDownloader, cache: ImageCache) {
_downloader = downloader
_cache = cache
let processQueueName = "com.onevcat.Kingfisher.KingfisherManager.processQueue.\(UUID().uuidString)"
processingQueue = .dispatch(DispatchQueue(label: processQueueName))
}
// MARK: - Getting Images
/// Retrieves an image from a specified resource.
///
/// - Parameters:
/// - resource: The ``Resource`` object defining data information, such as a key or URL.
/// - options: Options to use when creating the image.
/// - progressBlock: Called when the image download progress is updated. This block is invoked only if the response
/// contains an `expectedContentLength` and always runs on the main queue.
/// - downloadTaskUpdated: Called when a new image download task is created for the current image retrieval. This
/// typically occurs when an alternative source is used to replace the original (failed) task. You can update your
/// reference to the ``DownloadTask`` if you want to manually invoke ``DownloadTask/cancel()`` on the new task.
/// - completionHandler: Called when the image retrieval and setting are completed. This completion handler is
/// invoked from the `options.callbackQueue`. If not specified, the main queue is used.
///
/// - Returns: A task representing the image download. If a download task is initiated for a ``Source/network(_:)`` resource,
/// the started ``DownloadTask`` is returned; otherwise, `nil` is returned.
///
/// - Note: This method first checks whether the requested `resource` is already in the cache. If it is cached,
/// it returns `nil` and invokes the `completionHandler` after retrieving the cached image. Otherwise, it downloads
/// the `resource`, stores it in the cache, and then calls the `completionHandler`.
///
@discardableResult
public func retrieveImage(
with resource: any Resource,
options: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
downloadTaskUpdated: DownloadTaskUpdatedBlock? = nil,
completionHandler: (@Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
{
return retrieveImage(
with: resource.convertToSource(),
options: options,
progressBlock: progressBlock,
downloadTaskUpdated: downloadTaskUpdated,
completionHandler: completionHandler
)
}
/// Retrieves an image from a specified source.
///
/// - Parameters:
/// - source: The ``Source`` object defining data information, such as a key or URL.
/// - options: Options to use when creating the image.
/// - progressBlock: Called when the image download progress is updated. This block is invoked only if the response
/// contains an `expectedContentLength` and always runs on the main queue.
/// - downloadTaskUpdated: Called when a new image download task is created for the current image retrieval. This
/// typically occurs when an alternative source is used to replace the original (failed) task. You can update your
/// reference to the ``DownloadTask`` if you want to manually invoke ``DownloadTask/cancel()`` on the new task.
/// - completionHandler: Called when the image retrieval and setting are completed. This completion handler is
/// invoked from the `options.callbackQueue`. If not specified, the main queue is used.
///
/// - Returns: A task representing the image download. If a download task is initiated for a ``Source/network(_:)`` resource,
/// the started ``DownloadTask`` is returned; otherwise, `nil` is returned.
///
/// - Note: This method first checks whether the requested `source` is already in the cache. If it is cached,
/// it returns `nil` and invokes the `completionHandler` after retrieving the cached image. Otherwise, it downloads
/// the `source`, stores it in the cache, and then calls the `completionHandler`.
///
@discardableResult
public func retrieveImage(
with source: Source,
options: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
downloadTaskUpdated: DownloadTaskUpdatedBlock? = nil,
completionHandler: (@Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
{
let options = currentDefaultOptions + (options ?? .empty)
let info = KingfisherParsedOptionsInfo(options)
return retrieveImage(
with: source,
options: info,
progressBlock: progressBlock,
downloadTaskUpdated: downloadTaskUpdated,
completionHandler: completionHandler)
}
func retrieveImage(
with source: Source,
options: KingfisherParsedOptionsInfo,
progressBlock: DownloadProgressBlock? = nil,
downloadTaskUpdated: DownloadTaskUpdatedBlock? = nil,
completionHandler: (@Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
{
var info = options
if let block = progressBlock {
info.onDataReceived = (info.onDataReceived ?? []) + [ImageLoadingProgressSideEffect(block)]
}
return retrieveImage(
with: source,
options: info,
downloadTaskUpdated: downloadTaskUpdated,
progressiveImageSetter: nil,
completionHandler: completionHandler)
}
func retrieveImage(
with source: Source,
options: KingfisherParsedOptionsInfo,
downloadTaskUpdated: DownloadTaskUpdatedBlock? = nil,
progressiveImageSetter: ((KFCrossPlatformImage?) -> Void)? = nil,
referenceTaskIdentifierChecker: (() -> Bool)? = nil,
completionHandler: (@Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
{
var options = options
let retryStrategy = options.retryStrategy
if let provider = ImageProgressiveProvider(options: options, refresh: { image in
guard let setter = progressiveImageSetter else {
return
}
guard let strategy = options.progressiveJPEG?.onImageUpdated(image) else {
setter(image)
return
}
switch strategy {
case .default: setter(image)
case .keepCurrent: break
case .replace(let newImage): setter(newImage)
}
}) {
options.onDataReceived = (options.onDataReceived ?? []) + [provider]
}
if let checker = referenceTaskIdentifierChecker {
options.onDataReceived?.forEach {
$0.onShouldApply = checker
}
}
let retrievingContext = RetrievingContext(options: options, originalSource: source)
@Sendable func startNewRetrieveTask(
with source: Source,
retryContext: RetryContext?,
downloadTaskUpdated: DownloadTaskUpdatedBlock?
) {
let newTask = self.retrieveImage(with: source, context: retrievingContext) { result in
handler(currentSource: source, retryContext: retryContext, result: result)
}
downloadTaskUpdated?(newTask)
}
@Sendable func failCurrentSource(_ source: Source, retryContext: RetryContext?, with error: KingfisherError) {
// Skip alternative sources if the user cancelled it.
guard !error.isTaskCancelled else {
completionHandler?(.failure(error))
return
}
// When low data mode constrained error, retry with the low data mode source instead of use alternative on fly.
guard !error.isLowDataModeConstrained else {
if let source = retrievingContext.options.lowDataModeSource {
retrievingContext.options.lowDataModeSource = nil
startNewRetrieveTask(with: source, retryContext: retryContext, downloadTaskUpdated: downloadTaskUpdated)
} else {
// This should not happen.
completionHandler?(.failure(error))
}
return
}
if let nextSource = retrievingContext.popAlternativeSource() {
retrievingContext.appendError(error, to: source)
startNewRetrieveTask(with: nextSource, retryContext: retryContext, downloadTaskUpdated: downloadTaskUpdated)
} else {
// No other alternative source. Finish with error.
if retrievingContext.propagationErrors.isEmpty {
completionHandler?(.failure(error))
} else {
retrievingContext.appendError(error, to: source)
let finalError = KingfisherError.imageSettingError(
reason: .alternativeSourcesExhausted(retrievingContext.propagationErrors)
)
completionHandler?(.failure(finalError))
}
}
}
@Sendable func handler(
currentSource: Source,
retryContext: RetryContext?,
result: (Result<RetrieveImageResult, KingfisherError>)
) -> Void {
switch result {
case .success:
completionHandler?(result)
case .failure(let error):
if let retryStrategy = retryStrategy {
let context = retryContext?.increaseRetryCount() ?? RetryContext(source: source, error: error)
retryStrategy.retry(context: context) { decision in
switch decision {
case .retry(let userInfo):
context.userInfo = userInfo
startNewRetrieveTask(with: source, retryContext: context, downloadTaskUpdated: downloadTaskUpdated)
case .stop:
failCurrentSource(currentSource, retryContext: context, with: error)
}
}
} else {
failCurrentSource(currentSource, retryContext: retryContext, with: error)
}
}
}
return retrieveImage(
with: source,
context: retrievingContext)
{
result in
handler(currentSource: source, retryContext: nil, result: result)
}
}
private func retrieveImage(
with source: Source,
context: RetrievingContext<Source>,
completionHandler: (@Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
{
let options = context.options
if options.forceRefresh {
return loadAndCacheImage(
source: source,
context: context,
completionHandler: completionHandler)?.value
} else {
let loadedFromCache = retrieveImageFromCache(
source: source,
context: context,
completionHandler: completionHandler)
if loadedFromCache {
return nil
}
if options.onlyFromCache {
let error = KingfisherError.cacheError(reason: .imageNotExisting(key: source.cacheKey))
completionHandler?(.failure(error))
return nil
}
return loadAndCacheImage(
source: source,
context: context,
completionHandler: completionHandler)?.value
}
}
func provideImage(
provider: any ImageDataProvider,
options: KingfisherParsedOptionsInfo,
completionHandler: (@Sendable (Result<ImageLoadingResult, KingfisherError>) -> Void)?)
{
guard let completionHandler = completionHandler else { return }
provider.data { result in
switch result {
case .success(let data):
(options.processingQueue ?? self.processingQueue).execute {
let processor = options.processor
let processingItem = ImageProcessItem.data(data)
guard let image = processor.process(item: processingItem, options: options) else {
options.callbackQueue.execute {
let error = KingfisherError.processorError(
reason: .processingFailed(processor: processor, item: processingItem))
completionHandler(.failure(error))
}
return
}
options.callbackQueue.execute {
let result = ImageLoadingResult(image: image, url: nil, originalData: data)
completionHandler(.success(result))
}
}
case .failure(let error):
options.callbackQueue.execute {
let error = KingfisherError.imageSettingError(
reason: .dataProviderError(provider: provider, error: error))
completionHandler(.failure(error))
}
}
}
}
private func cacheImage(
source: Source,
options: KingfisherParsedOptionsInfo,
context: RetrievingContext<Source>,
result: Result<ImageLoadingResult, KingfisherError>,
completionHandler: (@Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)?
)
{
switch result {
case .success(let value):
let needToCacheOriginalImage = options.cacheOriginalImage &&
options.processor != DefaultImageProcessor.default
let coordinator = CacheCallbackCoordinator(
shouldWaitForCache: options.waitForCache, shouldCacheOriginal: needToCacheOriginalImage)
let result = RetrieveImageResult(
image: options.imageModifier?.modify(value.image) ?? value.image,
cacheType: .none,
source: source,
originalSource: context.originalSource,
data: { value.originalData }
)
// Add image to cache.
let targetCache = options.targetCache ?? self.cache
targetCache.store(
value.image,
original: value.originalData,
forKey: source.cacheKey,
options: options,
toDisk: !options.cacheMemoryOnly)
{
_ in
coordinator.apply(.cachingImage) {
completionHandler?(.success(result))
}
}
// Add original image to cache if necessary.
if needToCacheOriginalImage {
let originalCache = options.originalCache ?? targetCache
originalCache.storeToDisk(
value.originalData,
forKey: source.cacheKey,
processorIdentifier: DefaultImageProcessor.default.identifier,
expiration: options.diskCacheExpiration)
{
_ in
coordinator.apply(.cachingOriginalImage) {
completionHandler?(.success(result))
}
}
}
coordinator.apply(.cacheInitiated) {
completionHandler?(.success(result))
}
case .failure(let error):
completionHandler?(.failure(error))
}
}
@discardableResult
func loadAndCacheImage(
source: Source,
context: RetrievingContext<Source>,
completionHandler: (@Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask.WrappedTask?
{
let options = context.options
@Sendable func _cacheImage(_ result: Result<ImageLoadingResult, KingfisherError>) {
cacheImage(
source: source,
options: options,
context: context,
result: result,
completionHandler: completionHandler
)
}
switch source {
case .network(let resource):
let downloader = options.downloader ?? self.downloader
let task = downloader.downloadImage(
with: resource.downloadURL, options: options, completionHandler: _cacheImage
)
// The code below is neat, but it fails the Swift 5.2 compiler with a runtime crash when
// `BUILD_LIBRARY_FOR_DISTRIBUTION` is turned on. I believe it is a bug in the compiler.
// Let's fallback to a traditional style before it can be fixed in Swift.
//
// https://github.com/onevcat/Kingfisher/issues/1436
//
// return task.map(DownloadTask.WrappedTask.download)
if task.isInitialized {
return .download(task)
} else {
return nil
}
case .provider(let provider):
provideImage(provider: provider, options: options, completionHandler: _cacheImage)
return .dataProviding
}
}
/// Retrieves an image from either memory or disk cache.
///
/// - Parameters:
/// - source: The target source from which to retrieve the image.
/// - key: The key to use for caching the image.
/// - url: The image request URL. This is not used when retrieving an image from the cache; it is solely used for
/// compatibility with ``RetrieveImageResult`` callbacks.
/// - options: Options on how to retrieve the image from the image cache.
/// - completionHandler: Called when the image retrieval is complete, either with a successful
/// ``RetrieveImageResult`` or an error.
///
/// - Returns: `true` if the requested image or the original image before processing exists in the cache. Otherwise, this method returns `false`.
///
/// - Note: Image retrieval can occur in either the memory cache or the disk cache. The
/// ``KingfisherOptionsInfoItem/processor(_:)`` option in `options` is considered when searching the cache. If no
/// processed image is found, Kingfisher attempts to determine whether an original version of the image exists. If
/// an original exists, Kingfisher retrieves it from the cache and processes it. Subsequently, the processed image
/// is stored back in the cache for future use.
///
func retrieveImageFromCache(
source: Source,
context: RetrievingContext<Source>,
completionHandler: (@Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> Bool
{
let options = context.options
// 1. Check whether the image was already in target cache. If so, just get it.
let targetCache = options.targetCache ?? cache
let key = source.cacheKey
let targetImageCached = targetCache.imageCachedType(
forKey: key, processorIdentifier: options.processor.identifier)
let validCache = targetImageCached.cached &&
(options.fromMemoryCacheOrRefresh == false || targetImageCached == .memory)
if validCache {
targetCache.retrieveImage(forKey: key, options: options) { result in
guard let completionHandler = completionHandler else { return }
// TODO: Optimize it when we can use async across all the project.
@Sendable func checkResultImageAndCallback(_ inputImage: KFCrossPlatformImage) {
var image = inputImage
if image.kf.imageFrameCount != nil && image.kf.imageFrameCount != 1, let data = image.kf.animatedImageData {
// Always recreate animated image representation since it is possible to be loaded in different options.
// https://github.com/onevcat/Kingfisher/issues/1923
image = options.processor.process(item: .data(data), options: options) ?? .init()
}
if let modifier = options.imageModifier {
image = modifier.modify(image)
}
let value = result.map {
RetrieveImageResult(
image: image,
cacheType: $0.cacheType,
source: source,
originalSource: context.originalSource,
data: { [image] in options.cacheSerializer.data(with: image, original: nil) }
)
}
completionHandler(value)
}
result.match { cacheResult in
options.callbackQueue.execute {
guard let image = cacheResult.image else {
completionHandler(.failure(KingfisherError.cacheError(reason: .imageNotExisting(key: key))))
return
}
if options.cacheSerializer.originalDataUsed {
let processor = options.processor
(options.processingQueue ?? self.processingQueue).execute {
let item = ImageProcessItem.image(image)
guard let processedImage = processor.process(item: item, options: options) else {
let error = KingfisherError.processorError(
reason: .processingFailed(processor: processor, item: item))
options.callbackQueue.execute { completionHandler(.failure(error)) }
return
}
options.callbackQueue.execute {
checkResultImageAndCallback(processedImage)
}
}
} else {
checkResultImageAndCallback(image)
}
}
} onFailure: { error in
options.callbackQueue.execute {
completionHandler(.failure(KingfisherError.cacheError(reason: .imageNotExisting(key: key))))
}
}
}
return true
}
// 2. Check whether the original image exists. If so, get it, process it, save to storage and return.
let originalCache = options.originalCache ?? targetCache
// No need to store the same file in the same cache again.
if originalCache === targetCache && options.processor == DefaultImageProcessor.default {
return false
}
// Check whether the unprocessed image existing or not.
let originalImageCacheType = originalCache.imageCachedType(
forKey: key, processorIdentifier: DefaultImageProcessor.default.identifier)
let canAcceptDiskCache = !options.fromMemoryCacheOrRefresh
let canUseOriginalImageCache =
(canAcceptDiskCache && originalImageCacheType.cached) ||
(!canAcceptDiskCache && originalImageCacheType == .memory)
if canUseOriginalImageCache {
// Now we are ready to get found the original image from cache. We need the unprocessed image, so remove
// any processor from options first.
var optionsWithoutProcessor = options
optionsWithoutProcessor.processor = DefaultImageProcessor.default
originalCache.retrieveImage(forKey: key, options: optionsWithoutProcessor) { result in
result.match(
onSuccess: { cacheResult in
guard let image = cacheResult.image else {
assertionFailure("The image (under key: \(key) should be existing in the original cache.")
return
}
let processor = options.processor
(options.processingQueue ?? self.processingQueue).execute {
let item = ImageProcessItem.image(image)
guard let processedImage = processor.process(item: item, options: options) else {
let error = KingfisherError.processorError(
reason: .processingFailed(processor: processor, item: item))
options.callbackQueue.execute { completionHandler?(.failure(error)) }
return
}
var cacheOptions = options
cacheOptions.callbackQueue = .untouch
let coordinator = CacheCallbackCoordinator(
shouldWaitForCache: options.waitForCache, shouldCacheOriginal: false)
let image = options.imageModifier?.modify(processedImage) ?? processedImage
let result = RetrieveImageResult(
image: image,
cacheType: .none,
source: source,
originalSource: context.originalSource,
data: { options.cacheSerializer.data(with: processedImage, original: nil) }
)
targetCache.store(
processedImage,
forKey: key,
options: cacheOptions,
toDisk: !options.cacheMemoryOnly)
{
_ in
coordinator.apply(.cachingImage) {
options.callbackQueue.execute { completionHandler?(.success(result)) }
}
}
coordinator.apply(.cacheInitiated) {
options.callbackQueue.execute { completionHandler?(.success(result)) }
}
}
},
onFailure: { _ in
// This should not happen actually, since we already confirmed `originalImageCached` is `true`.
// Just in case...
options.callbackQueue.execute {
completionHandler?(
.failure(KingfisherError.cacheError(reason: .imageNotExisting(key: key)))
)
}
}
)
}
return true
}
return false
}
}
// Concurrency
extension KingfisherManager {
/// Retrieves an image from a specified resource.
///
/// - Parameters:
/// - resource: The ``Resource`` object defining data information, such as a key or URL.
/// - options: Options to use when creating the image.
/// - progressBlock: Called when the image download progress is updated. This block is invoked only if the response
/// contains an `expectedContentLength` and always runs on the main queue.
///
/// - Returns: The ``RetrieveImageResult`` containing the retrieved image object and cache type.
/// - Throws: A ``KingfisherError`` if any issue occurred during the image retrieving progress.
///
/// - Note: This method first checks whether the requested `resource` is already in the cache. If it is cached,
/// it returns `nil` and invokes the `completionHandler` after retrieving the cached image. Otherwise, it downloads
/// the `resource`, stores it in the cache, and then calls the `completionHandler`.
///
public func retrieveImage(
with resource: any Resource,
options: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil
) async throws -> RetrieveImageResult
{
try await retrieveImage(
with: resource.convertToSource(),
options: options,
progressBlock: progressBlock
)
}
/// Retrieves an image from a specified source.
///
/// - Parameters:
/// - source: The ``Source`` object defining data information, such as a key or URL.
/// - options: Options to use when creating the image.
/// - progressBlock: Called when the image download progress is updated. This block is invoked only if the response
/// contains an `expectedContentLength` and always runs on the main queue.
///
/// - Returns: The ``RetrieveImageResult`` containing the retrieved image object and cache type.
/// - Throws: A ``KingfisherError`` if any issue occurred during the image retrieving progress.
///
/// - Note: This method first checks whether the requested `source` is already in the cache. If it is cached,
/// it returns `nil` and invokes the `completionHandler` after retrieving the cached image. Otherwise, it downloads
/// the `source`, stores it in the cache, and then calls the `completionHandler`.
///
public func retrieveImage(
with source: Source,
options: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil
) async throws -> RetrieveImageResult
{
let options = currentDefaultOptions + (options ?? .empty)
let info = KingfisherParsedOptionsInfo(options)
return try await retrieveImage(
with: source,
options: info,
progressBlock: progressBlock
)
}
func retrieveImage(
with source: Source,
options: KingfisherParsedOptionsInfo,
progressBlock: DownloadProgressBlock? = nil
) async throws -> RetrieveImageResult
{
var info = options
if let block = progressBlock {
info.onDataReceived = (info.onDataReceived ?? []) + [ImageLoadingProgressSideEffect(block)]
}
return try await retrieveImage(
with: source,
options: info,
progressiveImageSetter: nil
)
}
func retrieveImage(
with source: Source,
options: KingfisherParsedOptionsInfo,
progressiveImageSetter: ((KFCrossPlatformImage?) -> Void)? = nil,
referenceTaskIdentifierChecker: (() -> Bool)? = nil
) async throws -> RetrieveImageResult
{
let task = CancellationDownloadTask()
return try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation { continuation in
let downloadTask = retrieveImage(
with: source,
options: options,
downloadTaskUpdated: { newTask in
Task {
await task.setTask(newTask)
}
},
progressiveImageSetter: progressiveImageSetter,
referenceTaskIdentifierChecker: referenceTaskIdentifierChecker,
completionHandler: { result in
continuation.resume(with: result)
}
)
if Task.isCancelled {
downloadTask?.cancel()
} else {
Task {
await task.setTask(downloadTask)
}
}
}
} onCancel: {
Task {
await task.task?.cancel()
}
}
}
}
class RetrievingContext<SourceType>: @unchecked Sendable {
private let propertyQueue = DispatchQueue(label: "com.onevcat.Kingfisher.RetrievingContextPropertyQueue")
private var _options: KingfisherParsedOptionsInfo
var options: KingfisherParsedOptionsInfo {
get { propertyQueue.sync { _options } }
set { propertyQueue.sync { _options = newValue } }
}
let originalSource: SourceType
var propagationErrors: [PropagationError] = []
init(options: KingfisherParsedOptionsInfo, originalSource: SourceType) {
self.originalSource = originalSource
_options = options
}
func popAlternativeSource() -> Source? {
var localOptions = options
guard var alternativeSources = localOptions.alternativeSources, !alternativeSources.isEmpty else {
return nil
}
let nextSource = alternativeSources.removeFirst()
localOptions.alternativeSources = alternativeSources
options = localOptions
return nextSource
}
@discardableResult
func appendError(_ error: KingfisherError, to source: Source) -> [PropagationError] {
let item = PropagationError(source: source, error: error)
propagationErrors.append(item)
return propagationErrors
}
}
class CacheCallbackCoordinator: @unchecked Sendable {
enum State {
case idle
case imageCached
case originalImageCached
case done
}
enum Action {
case cacheInitiated
case cachingImage
case cachingOriginalImage
}
private let shouldWaitForCache: Bool
private let shouldCacheOriginal: Bool
private let stateQueue: DispatchQueue
private var threadSafeState: State = .idle
private(set) var state: State {
set { stateQueue.sync { threadSafeState = newValue } }
get { stateQueue.sync { threadSafeState } }
}
init(shouldWaitForCache: Bool, shouldCacheOriginal: Bool) {
self.shouldWaitForCache = shouldWaitForCache
self.shouldCacheOriginal = shouldCacheOriginal
let stateQueueName = "com.onevcat.Kingfisher.CacheCallbackCoordinator.stateQueue.\(UUID().uuidString)"
self.stateQueue = DispatchQueue(label: stateQueueName)
}
func apply(_ action: Action, trigger: () -> Void) {
switch (state, action) {
case (.done, _):
break
// From .idle
case (.idle, .cacheInitiated):
if !shouldWaitForCache {
state = .done
trigger()
}
case (.idle, .cachingImage):
if shouldCacheOriginal {
state = .imageCached
} else {
state = .done
trigger()
}
case (.idle, .cachingOriginalImage):
state = .originalImageCached
// From .imageCached
case (.imageCached, .cachingOriginalImage):
state = .done
trigger()
// From .originalImageCached
case (.originalImageCached, .cachingImage):
state = .done
trigger()
default:
assertionFailure("This case should not happen in CacheCallbackCoordinator: \(state) - \(action)")
}
}
}