How to watch changement of firebase storage and update the viewcontroller on real time?

Viewed 979

I'm building an app where a user can update his profile picture using firebase services (firebase storage).

In my app user can have multiple friends, and pictures of his friends is displayed in a view controller.

What I want to achieve is that when user updates his profile picture all his friends (connected using other devices) get his image changed in a real time.

For that I believe that the easiest solution is to watch the changes in the storage.

Here is the code I used to upload the user image

 let storage = FIRStorage.storage()
                    let storageref = storage.reference(forURL: "my firebase url")
                    let imageref = storageref.child("images")
                    let userid : String = (user.uid)
                    let spaceref = imageref.child("\(userid).jpg")

                    let imageConverter = ImageConverter()
                    var profilePicture : UIImage = imageConverter.cropToBounds(image: self.profilePicture.image!, width: 150 ,height: 150)
                    profilePicture = imageConverter.resizeImage(image: profilePicture, targetSize: CGSize(width: 140, height: 150))
                    let imageData = UIImagePNGRepresentation(profilePicture)
                    _ = spaceref.put(imageData!, metadata: nil){ metadata, error in
                        if (error != nil) {
                            loadingScreenViewController.view.removeFromSuperview()
                            self.alert(title: "Error", description: "please check your internet connection and try again")
                        }
                        else {
                            _ = metadata!.downloadURL
                        }

                    loadingScreenViewController.view.removeFromSuperview()

Does anyone have an idea about how to watch changement in firebase storage.

I get the image simply by using

let imageRef = storage.child("images/\(friendsManager.friends[indexPath.row].id).jpg")
1 Answers

As far as I know Firebase doesn't support 'watching for changes' in the Storage module. The best way around this would be to store the URL to the profile images in the Realtime database, then listen for changes there and download the images accordingly. Use a randomly generated string for each image so you can ensure they're unique. Something like -

 _ = spaceref.put(imageData!, metadata: nil){ metadata, error in
      if (error != nil) {
             loadingScreenViewController.view.removeFromSuperview()
             self.alert(title: "Error", description: "please check your internet connection and try again")
      } else {
             if let downloadUrl = metadata.downloadURL() {
                 let db = Database.database().reference()
                 db.child("profiles").child("myuserid").setValue(["photoUrl" : downloadUrl])
             }
      }

Then watch for changes -

profilesRef.observe(.childChanged, with: { (snapshot) -> Void in
  // Download the image from the stored url
})
Related