Unwind Segue Causes Crash

I am creating an app with a tab bar. (4 Tabs)


In the add page (3rd Tab) when I click on a button it triggers an IB Action.


Here's IB Action Code


if type3 != "" {
            newWine.type = type3
            type3 = ""
        }
        
       var ok = 0
        
        if locationField.text!.count <= 35 {
           
            newWine.name = locationField.text!
            ok = ok + 1
        } else {
            SVProgressHUD.showError(withStatus: "Grape Origin Can Be A Maximum of 35 Characters")
        }
        if brandTextField.text!.count <= 30 {
            
            newWine.brand = brandTextField.text!
            ok = ok + 1
        } else {
            SVProgressHUD.showError(withStatus: "Brand Can Be A Maximum of 30 Characters")
        }
        
        if tastingLocation.text!.count <= 35 {
            
            newWine.tastingLocation = tastingLocation.text!
            ok = ok + 1
        } else {
            SVProgressHUD.showError(withStatus: "Tasting Location Can Be A Maximum of 35 Characters")
        }
        
        if ratingTextField.text!.count <= 3 {
            if ratingTextField.text! == "1" {
                newWine.rating = ratingTextField.text!
                ok = ok + 1
            }
            else if ratingTextField.text! == "1.5" {
                newWine.rating = ratingTextField.text!
                ok = ok + 1
            }
            else if ratingTextField.text! == "2" {
                newWine.rating = ratingTextField.text!
                ok = ok + 1
            }
            else if ratingTextField.text! == "2.5" {
                newWine.rating = ratingTextField.text!
                ok = ok + 1
            }
            else if ratingTextField.text! == "3" {
                newWine.rating = ratingTextField.text!
                ok = ok + 1
            }
            else if ratingTextField.text! == "3.5" {
                newWine.rating = ratingTextField.text!
                ok = ok + 1
            }
            else if ratingTextField.text! == "4" {
                newWine.rating = ratingTextField.text!
                ok = ok + 1
            }
            else if ratingTextField.text! == "4.5" {
                newWine.rating = ratingTextField.text!
                ok = ok + 1
            }
            else if ratingTextField.text! == "5" {
                newWine.rating = ratingTextField.text!
                ok = ok + 1
            }
            else if ratingTextField.text! == "" {
                newWine.rating = ratingTextField.text!
                ok = ok + 1
            }
            else {
                SVProgressHUD.showError(withStatus: "Rating Is A Whole Number Like 1 or A Half Number Like 1.5 ")
            }
           
        } else {
            SVProgressHUD.showError(withStatus: "Rating is a Scale of 1-5")
        }
        if yearTextField.text!.count <= 4 {
            
              newWine.year = yearTextField.text!
            ok = ok + 1
        } else {
            SVProgressHUD.showError(withStatus: "Year is a 4 Digit Number")
        }
        
        if ok >= 5 {
            
            //      newWine.type = typeTextField.text!
            lastAddedWine = newWine
            
            
           
            newWine.notes = note2.text!
            newWine.pictureNumber = Int(number)
            
            //        todoItems.append(newWine)
            let saving = ViewController()
            saving.saveItems(newWine: newWine)
          
            performSegue(withIdentifier: "backToList", sender: self)



Here is the saveItems function:


func saveItems(newWine: Wine) {
        do {
            try realm.write {
                realm.add(newWine)
                
            }
        } catch {
            print("Error saving category \(error)")
        }
    }
   



But when the IBAction gets triggered a second time the app crashes with a Terminating app due to uncaught exception 'RLMException', reason: 'Attempting to modify object outside of a write transaction - call beginWriteTransaction on an RLMRealm instance first.'



Thanks for all help!

Replies

Where do you get the crash exactly ?


Why do you say the crash is due to unwind segue ?


Could you instrument code to check where you crach:


func saveItems(newWine: Wine) {
     print("will save")
        do {
            try realm.write {
               print("before add")
                realm.add(newWine)
               print("after add")

            }
        } catch {
            print("Error saving category \(error)")
        }
    }


Note: That's probably more a question for realm support than for this forum.