0

I have a reference to a project image in Firebase, and I want to edit the photo by replacing the data in the storage reference, without changing the reference link. I am successfully doing this, and the image view is updating as well when a user selects a new image. However, when I go back in the navigation controller, my tableView still shows the same image - and when I click on the tableView item - the project image does not seem to be updated.

The only way the image and tableview data successfully update is when I delete the simulator from my computer and re-add it. Here is some of my code for how I populate the tableView - the code works great for new projects being added but the modified portion is not working - I call this function in viewwillappear to populate the tableview:

func checkForUpdates(uid: String) {

    handler = db.collection("Projects").whereField("UID", isEqualTo: "\(uid)").addSnapshotListener { snapShot, error in

    guard let document = snapShot else {return}

        if document.count > 0 {

    document.documentChanges.forEach { difference in

        if difference.type == .added {

            if let project = Project(dictionary: difference.document.data()) {

                self.projectArray.append(project)

                DispatchQueue.main.async {

                    self.tableView.reloadData()
                }
            }

            else {return}

            }

        if difference.type == .modified {

            DispatchQueue.main.async {

                self.loadData(uid: uid)
                self.tableView.reloadData()
            }

            return
        }

        if difference.type == .removed {return}

            else {return}
        }
    }

        else {

            print("no documents to show")
        }
    }
}

When I segue into viewing a particular project (by clicking on tableviewcell) - here is my code

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "viewProject" {

        if let vc = segue.destination as? ViewMyProject {

            vc.selectedProject = self.selectedProject!
        }

        else {return}    
    }           
    else {return}
}

Here is my code for how I set cells within a xib file

func setProject(project: Project) {

    projectName.text = project.title
    projectCategory.text = project.category
    projectDate.text = project.timeStamp.dateValue().dateToString(style: .short)
    projectImage!.sd_setImage(with: URL.init(string: project.imageLink)) { (image, error, cacheType, url) in

        if error != nil {

            print ("Error setting project cell image")
            return
        }

        else {

            print("Successfuly set project cell image")
        }
    }
}

And here is how I load the project after clicking on the cell (I call this in viewWillAppear)

func loadProject(project: Project) {

    filterProjectFeedback(project: project)

    if projectImage.image == nil {projectImage.sd_setImage(with: URL.init(string: project.imageLink))}

    projectDescription.text = project.description
}

Thanks so much in advance - I have a feeling it has to do with a strong reference - but I cannot make my "projectArray" variable weak

Pawel Czuczwara
  • 1,442
  • 9
  • 20
Jeff Small
  • 91
  • 1
  • 5
  • Hey guys! Just giving a quick update : the issue lies with the cacheing featured in SDWebImage. I am now clearing the cache (globally) each time I press on edit image, and the image is updating properly. However, I would imagine this is not the swiftest solution - any other suggestions? – Jeff Small Jul 01 '19 at 23:37
  • Did you try any of those [solutions proposed to update cache SDWebImage](https://stackoverflow.com/questions/28586331/how-to-update-image-in-cache-when-image-changed-on-server-with-sdwebimage) Especially SDWebImageRefreshCached ? – Pawel Czuczwara Jul 03 '19 at 10:33

1 Answers1

0

Here is a code in swift 3 to refresh cache everytime:

imgCardBack.sd_setImage(with: URL(string: objUserData.back_image!), placeholderImage:UIImage(named: "cardBack"), options: .refreshCached)

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