Bladeren bron

Add missing resource error for disk cache

Normally this should not happen, but in case.
onevcat 1 jaar geleden
bovenliggende
commit
b9c4a7e0ac
2 gewijzigde bestanden met toevoegingen van 21 en 6 verwijderingen
  1. 11 0
      Sources/General/KingfisherError.swift
  2. 10 6
      Sources/General/KingfisherManager+LivePhoto.swift

+ 11 - 0
Sources/General/KingfisherError.swift

@@ -243,6 +243,13 @@ public enum KingfisherError: Error {
         ///
         ///
         /// Error Code: 3011
         /// Error Code: 3011
         case diskStorageIsNotReady(cacheURL: URL)
         case diskStorageIsNotReady(cacheURL: URL)
+        
+        /// The resource is expected on the disk, but now missing for some reason.
+        ///
+        /// This happens when the expected resource is not on the disk for some reason during loading a live photo.
+        ///
+        /// Error Code: 3012
+        case missingLivePhotoResourceOnDisk(_ resource: LivePhotoResource)
     }
     }
     
     
     /// Represents the error reason during image processing phase.
     /// Represents the error reason during image processing phase.
@@ -570,6 +577,9 @@ extension KingfisherError.CacheErrorReason {
         case .diskStorageIsNotReady(let cacheURL):
         case .diskStorageIsNotReady(let cacheURL):
             return "The disk storage is not ready to use yet at URL: '\(cacheURL)'. " +
             return "The disk storage is not ready to use yet at URL: '\(cacheURL)'. " +
                 "This is usually caused by extremely lack of disk space. Ask users to free up some space and restart the app."
                 "This is usually caused by extremely lack of disk space. Ask users to free up some space and restart the app."
+        case .missingLivePhotoResourceOnDisk(let resource):
+            return "The live photo resource '\(resource.downloadURL)' is missing in the cache. Usually a re-download" +
+            " can fix this issue."
         }
         }
     }
     }
     
     
@@ -586,6 +596,7 @@ extension KingfisherError.CacheErrorReason {
         case .cannotCreateCacheFile: return 3009
         case .cannotCreateCacheFile: return 3009
         case .cannotSetCacheFileAttribute: return 3010
         case .cannotSetCacheFileAttribute: return 3010
         case .diskStorageIsNotReady: return 3011
         case .diskStorageIsNotReady: return 3011
+        case .missingLivePhotoResourceOnDisk: return 3012
         }
         }
     }
     }
 }
 }

+ 10 - 6
Sources/General/KingfisherManager+LivePhoto.swift

@@ -89,14 +89,18 @@ extension KingfisherManager {
         let resourcesResult = try await downloadAndCache(resources: missingResources, options: checkedOptions)
         let resourcesResult = try await downloadAndCache(resources: missingResources, options: checkedOptions)
         
         
         let targetCache = checkedOptions.targetCache ?? cache
         let targetCache = checkedOptions.targetCache ?? cache
-        let fileURLs = source.resources.map {
-            targetCache.possibleCacheFileURLIfOnDisk(resource: $0, options: checkedOptions)
-        }
-        if fileURLs.contains(nil) {
-            // not all file done. throw error
+        var fileURLs = [URL]()
+        for resource in source.resources {
+            let url = targetCache.possibleCacheFileURLIfOnDisk(resource: resource, options: checkedOptions)
+            guard let url else {
+                // This should not happen normally if the previous `downloadAndCache` done without issue, but in case.
+                throw KingfisherError.cacheError(reason: .missingLivePhotoResourceOnDisk(resource))
+            }
+            fileURLs.append(url)
         }
         }
+        
         return LivePhotoLoadingInfoResult(
         return LivePhotoLoadingInfoResult(
-            fileURLs: fileURLs.compactMap { $0 },
+            fileURLs: fileURLs,
             cacheType: missingResources.isEmpty ? .disk : .none,
             cacheType: missingResources.isEmpty ? .disk : .none,
             source: source,
             source: source,
             originalSource: source,
             originalSource: source,