Kingfisher: Images from Firebase url are not being cached

Hello! I have a little problem… For some reason images from Firebase url are not cached. What should be the problem? I tried with your example url, they were cached but like this they are not cached:

let productImageref = productsValue[indexPath.row]["Products"] as? String

    cell.snusProductImageView.image = nil
    cell.snusProductImageView.kf_showIndicatorWhenLoading = true



    FIRStorage.storage().reference().child("\(productImageref!).png").downloadURLWithCompletion({(url, error)in

        if error != nil{
            print(error)
            return
        }else{

            cell.snusProductImageView.kf_setImageWithURL(url, placeholderImage: nil,
                optionsInfo: [.Transition(ImageTransition.Fade(1))],
                progressBlock: { receivedSize, totalSize in
                    print("\(indexPath.row + 1): \(receivedSize)/\(totalSize)")
                    print(url)
                },
                completionHandler: { image, error, cacheType, imageURL in
                    print("\(indexPath.row + 1): Finished")
                    print(url)
            })
        }
    })

Can you say why it is like this?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 18 (9 by maintainers)

Most upvoted comments

@MaeseppTarvo Hi, I just tried your demo.

It is not related to the cache control settings in the response, Kingfisher has its own cache layer so it will be simply ignored. The problem is Kingfisher is using the full url for cache key by default, so you need to have the same url for that url or the cache could not be retrieved correctly. In your app, the image url is appended by a varying token every time, so Kingfisher sees different url and since that url is not in cache, Kingfisher will download it again.

Instead of using the url as the key, you could choose to create a resource object and use your product id (or something could identify your image) as the cache key. Then, load the resource instead of plain url, to make Kingfisher search/cache for that key.

Some code below based on your current implementation, hope it could help:

let productImageref = productsValue[indexPath.row]["Products"] as? String
FIRStorage.storage().reference().child("\(productImageref!).png").downloadURLWithCompletion({(url, error)in
            guard let url = url else {
                return
            }
            let resource = Resource(downloadURL: url, cacheKey: productImageref)
            cell.snusProductImageView.kf_setImageWithResource(resource)
        })

I know this is closed, but as @lustigdev said, you can and may want to set Cache-Control metadata when uploading images to Firebase Storage. The default setting (as 2018 March) is Private, max-age=0, thus no cache.

Search for metadata in the following official docs for more information how to upload files with customized metadata.

Here’s a the list of metadata properties that you can set