Search code examples
swiftswiftuikingfisher

Kingfisher implementation of offline support


The media is stored in Firebase Storage(cloud bucket), getting the url requires an internet connection(MediaManager.shared.generateURL). So even though they are stored in cache, the url is unknown when you are offline.

Now I can get the image from cache manually, because the cacheKey is known, store it in this struct using a variable and use the UIImage component. But there should be a way that just by knowing the key, the view modifier .url(url, cacheKey: cacheKey) should get it, right?

So how can I initialize the KFImage from just the cache key, as this is the only thing known offline.

Any thoughts or tips are appreciated!

struct SomeImage: View {
    var body: some View {
        KFImage
            .url(url, cacheKey: cacheKey)
            .diskCacheExpiration(.days(14))
            .resizable()
            .fade(duration: 0.25)
            .placeholder {
                AnyView(placeholder())
            }
            .task {
                let cache = ImageCache.default
                if cache.isCached(forKey: cacheKey) {
                } else {
                    print("Key doesnt exist")
                    MediaManager.shared.generateURL(id: id, folder: folder) { downloadURL in
                        url = downloadURL
                    }
                }
            }
    }

--- UPDATE ---

Using an caching mechanism is the only option


Solution

  • Using a caching mechanism to store the urls to disk is the only option