Current User Firebase does not work properly after relogging
<p>I got a sign in and sign out method in my application. When the user registers for the first time, A unique is automatically generated by firebase and I store this in the userdefaults:</p>

<pre><code>let token = user?.uid
UserDefaults.standard.set(token, forKey: KEY_UID)
</code></pre>

<p>When this happens the first time and I print the userdefault and CURRENT_USER in the next ViewController: KEY_UID and CURRENT_USER both prints correctly.</p>

<pre><code>print("THE CURRENT USER IS: \(CURRENT_USER)")
print("THE UNIQUE ID IS: \(UserDefaults.standard.value(forKey: KEY_UID))")
</code></pre>

<p>I got a <strong>constants.swift</strong> file where I make the userdefault: KEY_UID equal to var CURRENT_USER so I can use it globally in my application.</p>

<pre><code>// USER DEFAULTS
var CURRENT_USER = UserDefaults.standard.value(forKey: KEY_UID)
</code></pre>

<p><strong>The problem happens when I click the sign out button and try to login again:</strong></p>

<pre><code>@IBAction func signOutBtnPressed(_ sender: AnyObject) {
        CURRENT_USER = nil
        UserDefaults.standard.removeObject(forKey: KEY_UID)
        try! FIRAuth.auth()!.signOut()
        performSegue(withIdentifier: "logoutSegue", sender: nil)
    }
</code></pre>

<p><strong>- When I login again, I take the user.uid again and set a new userDefault for it again:</strong></p>

<pre><code>// Login Button Pressed
        @IBAction func loginBtnPressed(_ sender: AnyObject) {

        FIRAuth.auth()?.signIn(withEmail: email, password: pwd, completion: { (user, error) in

            if error == nil {
                let token = user?.uid
                UserDefaults.standard.synchronize()
                CURRENT_USER = UserDefaults.standard.value(forKey: KEY_UID)
                self.perform(#selector(ViewController.goToMuscleListVC), with: 1, afterDelay: 2)
            }else{
                self.showErrorAlert(title: "Wrong Email or Password", msg: "Please fill in a valid account or create a new account on the bottom.")
            }
        }) // end sign in
</code></pre>

<p>EDIT</p>

<p>Had to make a new ref to the REF_WORKOUT_USER because the old ref still contains the old CURRENT_USER value. </p>

<p>The strange part right now is when I try to print the CURRENT_USER And the KEY_UID on the next ViewController it does print the KEY_UID and the CURRENT_USER to the new values of the new logged in account. When I do a new call to the firebase:</p>

<pre><code>private var _REF_WORKOUT_USER = DB_BASE.child("users").child("\(CURRENT_USER!)").child("workouts")
</code></pre>

<p><em>This CURRENT_USER is still the old one and I have no clue how this is possbile.</em></p>

<p>What Am I doing wrong at the moment? And why does it work when I restart the app?</p>

<p><strong>EDIT: ANSWER</strong>
<strong>New functioN:</strong></p>

<pre><code>// Creating account on firebase with email and password
    func createFirebaseDBUser(uid: String, userData: Dictionary&lt;String, String&gt;) {
        REF_USERS.child(uid).updateChildValues(userData)
    }

    // Creates a workout for the user with the exercises selected in exerciseVC.swift
    func CreateWorkoutForUser(baseValues: Dictionary&lt;String, Int&gt;, exerciseNames: [String]){

        print("CURRENT USER before making an workout: \(CURRENT_USER)")

        // grabbing the unique key thats generated at button click due firebase
        let uniqueKey = DB_BASE.child("users").child("\(CURRENT_USER!)").child("workouts").childByAutoId().key

        // appending done and value false to that unique id
        DB_BASE.child("users").child("\(CURRENT_USER!)").child("workouts").child(uniqueKey).child("Done").setValue("false")

        // iterate trought the exercises and for each key:value in the baseValues array set the key:value for the unique id firebase sets on the top
        for ex in exerciseNames {
            for (myKey, myValue) in baseValues {
                DB_BASE.child("users").child("\(CURRENT_USER!)").child("workouts").child(uniqueKey).child("exercise").child(ex).child(myKey).setValue(myValue)
            }
        }
    }
</code></pre>

<p>I really appreciate any help guys!</p>

<p>Cheers. :D</p>