0

I've been having trouble getting my SDWebImage cache to update when an image is updated on my firebase storage backend. As suggested in other answers I've read here on stack exchange I've implemented .refreshCached but still the same image seems to be loaded after the server has been updated. To confirm that it is a cache problem I have tested downloading the image uncached and have received the correct results. Any ideas of what I could be doing wrong or a better caching library. Thank you!

I understand that the problem stems from updating the same URL that is cached but is there a way to detect when the data has been changed on the server.

SDWebImage, Swift: SDWebImageRefreshCached unresolved identifier

How to update image in cache when image changed on server with SDWebImage

Firebase Storage Tutorial Using sd_setImage

My Implementation

 self.userProfileImage.sd_setImage(with: URL(string: user.photoURL), placeholderImage: placeHolder, options: SDWebImageOptions.refreshCached, completed: {image,error,imageCacheType,storageRef in
                    if let error = error{
                        print("Error during initial cache load 1: \(error)")
                    }
 })

2 Answers2

1

There is no notification mechanism in Cloud Storage (for Firebase) for changes to files. So the only way to ensure you get the latest data for the file right now is to not cache it.

Alternatively many developers use either the Firebase Realtime Database or Cloud Firestore to store some metadata for each file, and then update that metadata whenever the file changes. By attaching a listener to the metadata for the file in the database (see examples for Realtime Database, and Cloud Firestore) you can then force a reload of the image.

You could even go one step further and use Cloud Functions to detect when a file is written, and then update the metadata for that file from there. This means the app that uploads the image doesn't have to update the database itself, because Cloud Functions does it behind the scenes.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
1

Try the below code. In this, you need to set the image from the completion block.

self.imageView.sd_setImage(with: URL(string: url)!, placeholderImage: nil, options: .refreshCached) { (image, error, cacheType, url) in
            self.imageView.image = image
        }