Group Group Group Group Group Group Group Group Group

Chapter 12 - No view updates when popping the views

Hi there,

I have the problem that my views don’t get updated when I’m editing or creating an acronym.

For example when I click on SAVE after I finished editing an acronym the EditView gets popped and the DetailView appears. But on that I still see the old values.

The same when I create a new Acronym: after I created one and saved it, the CreateView pops and the overview appears. But on that my newly created acronym doesn’t appear at first. I have to “slide down to update” and then I see it.

What am I missing?

Can you show the code you’re using for saving? Also, are you models structs?

I use the same code as in the book. This is the save method:

@IBAction func save(_ sender: UIBarButtonItem) {
    
    print("acronym saved")
    
    guard let shortText = self.acronymShortTextField.text, !shortText.isEmpty else {
      ErrorPresenter.showError(message: "Definiere ein Acronym", on: self)
      return
    }
    
    guard let longText = self.acronymLongTextField.text, !longText.isEmpty else {
      ErrorPresenter.showError(message: "Gib eine Definition an", on: self)
      return
    }
    
    guard let userID = self.selectedUser?.id else {
      let message = "Wähle einen Nutzer in der Liste aus"
      ErrorPresenter.showError(message: message, on: self)
      return
    }
    
    let acronym:Acronym = Acronym(short: shortText, long: longText, userID: userID)
    print(acronym.long)
    
    if self.acronym != nil {
      print("Wir ändern")
      guard let existingID = self.acronym?.id else {
        let message = "Es gab einen Fehler beim Updaten des Acronyms"
        ErrorPresenter.showError(message: message, on: self)
        return
      }
      AcronymRequest(acronymID: existingID).update(with: acronym, completion: { result in
        switch result {
        case .failure:
          let message = "Es gab ein Problem beim Speichern der Änderung"
          ErrorPresenter.showError(message: message, on: self)
        case .success(let updatedAcronym):
          self.acronym = updatedAcronym
          DispatchQueue.main.async { [weak self] in
            print("unwinding")
            self?.performSegue(withIdentifier: "UpdateAcronymDetails", sender: nil)
          }
        }
      })
    } else {
      print("Wir erzeugen ein Neues")
      ResourceRequest<Acronym>(resourcePath: "acronyms").save(acronym, completion: { [weak self] result in
        switch result {
        case .failure:
          let message = "Es gab ein Problem beim Speichern des Acronyms"
          ErrorPresenter.showError(message: message, on: self)
        case .success:
          DispatchQueue.main.async { [weak self] in
            self?.navigationController?.popViewController(animated: true)
          }
        }
      })
    }
    
    navigationController?.popViewController(animated: true)
  }

The Print message “unwinding” (the segue) appears which means the Editing worked. And I think then the @IBAction func updateAcronymDetails(_ segue: UIStoryboardSegue) of the AcronymDetailTableViewController should get called to update the cells of the table views but that’s not the case.

Argh, oh my goodness. It was because of the last line of my save method where it popped the current controller. It shouldn’t have been there… now everything works just fine.
And I guess I still haven’t understood “DispatchQueue.main.async()” quite much :wink:

Glad it’s working! :smiley: