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