Realm. Update data within multiple ViewControllers

Viewed 49

I'm trying to create an App, that takes a initial condition and save it in Realm database. Here is the code for my Realm class:

class FunctionData: Object {
    @objc dynamic var Name = ""
    @objc dynamic var status = false
    @objc dynamic var statusComment = ""
    @objc dynamic var tester = ""
    
    override static func primaryKey() -> String? {
        return "functionNames"
    }
}

I created a TableViewController with the "Name" as the name for each Cell. I want to click on the cell and go to the "SecondViewController" and there, fill the other Information, like tester and Comment. The TableViewController works well. here the code, to make the segue to the SecondViewController:

var functions: Results<FunctionData>?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            
            tableView.deselectRow(at: indexPath, animated: true)
            let function = functions![indexPath.row]
            
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let functionsVC = storyboard.instantiateViewController(identifier: "functionsVC") as! FunctionsRatingViewController
            functionsVC.name = function.functionNames
            functionsVC.functionID = function.functionNames

if let indexPath = tableView.indexPathForSelectedRow {
                functionsVC.selectedFunction = functions![indexPath.row]
            }
            
            self.present(functionsVC, animated: true, completion: nil)
        
    }

But in my SecondVC I am not able to save the other Information in the selected "Item". I'm trying to do that with a AlertController

var selectedFunction: FunctionData? {
        didSet {
        }
    }


func showAlert() {
        
        var textField = UITextField()
        
        let alert = UIAlertController(title: "Status not OK", message: nil, preferredStyle: .alert)

            alert.addTextField {
                $0.placeholder = "Comment"
                $0.addTarget(alert, action: #selector(alert.textDidChangeInLoginAlert), for: .editingChanged)
            }
        
            alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))

            let loginAction = UIAlertAction(title: "Save", style: .default) { [unowned self] _ in
                
                    if let currentFct = self.selectedFunction {
                        do {
                            try self.realm.write {
                                currentFct.realm?.create(FunctionData.self, value: ["statusComment": textField.text], update: .modified)
                            }
                        } catch {
                            print("Error trying to append a Comment, \(error)")
                        }
                    }
 print("Comment saved")
 
            }
            loginAction.isEnabled = false
            alert.addAction(loginAction)
            present(alert, animated: true)
        }

Thanks a lot for any help!

1 Answers

The primary key on your Realm object is showing to be return "functionNames"

class FunctionData: Object {
    @objc dynamic var Name = ""
    @objc dynamic var status = false
    @objc dynamic var statusComment = ""
    @objc dynamic var tester = ""
    
    override static func primaryKey() -> String? {
        return "functionNames"
    }
}

but you've not included that property in the object, so it won't work.

Update your object to include that property

class FunctionData: Object {
    @objc dynamic var functionName = UUID().uuidString //for example
    @objc dynamic var Name = ""
    @objc dynamic var status = false
    @objc dynamic var statusComment = ""
    @objc dynamic var tester = ""
    
    override static func primaryKey() -> String? {
        return "functionNames"
    }
}

Also, primary keys are unique and singular so calling it functionNames is probably not the best move. Try functionName or even, and more appropriately function_id.

There may be additional issues but that's the first that should be addressed.

Related