how to save a basic Int Value once user quit apps

would someone kindly show me how to save an Int value once my apps is close? I have tried to save the count value to intKey once the user exit but it not working. When the user login again, the value in intKey will be put to count so whenever the user enters new data, it will be saved to firebase with a new id so that way data won't overwrite my previous data


import UIKit
import Firebase
import FirebaseDatabase

class SecondViewController: UIViewController {
      //label and textbox outlets
    @IBOutlet weak var inputText: UITextField!
    @IBOutlet weak var textLabel: UILabel!
   
    //var ref: DatabaseReference!
    var ref: DatabaseReference!
    var count: Int = 1
   
   
   //in this func, I save the count value once the user exit
    override func viewDidLoad() {
        super.viewDidLoad()
         ref = Database.database().reference()
     textLabel.text = ""
       
        //Set
        UserDefaults.standard.set(count, forKey: "intKey")
        //Get
      count =  UserDefaults.standard.integer(forKey: "intKey")
       
    }
   
   //log the user out
    @IBAction func LogoutTapped(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }
   
//allow user to save data 
    @IBAction func saveData(_ sender: Any) {
      
        count = count + 1
        var identifier: String = "user/data" + String(count)
       
        if inputText.text == ""{
           textLabel.text = "please enter text to continue!"
        } else {
            let inputData = inputText.text
            self.ref.child(identifier).setValue(inputData)
            inputText.text = ""
        }
     
      
       
    }
   
}//end of class

Replies

never mind i got it. replace self.ref.child(identifier).setValue(inputData) with


self.ref.child("newDada").childByAutoId().setValue(inputData)

and now the new id will be randomly generated