title: User Default Values Changing to Previous Values after some time - Swift
body: <p>Please don't mark my question duplicate.
  Gone through the following links and tried what they mentioned but no luck. </p>
  
  <p><a href="https://stackoverflow.com/questions/42174609/userdefaults-in-ios-10-is-sometimes-showing-old-value">UserDefaults in IOS 10 is sometimes showing old value</a></p>
  
  <p><a href="https://stackoverflow.com/questions/38758969/user-default-values-changing-to-previous-values-seemingly-randomly-swift">User Default Values Changing to Previous Values Seemingly Randomly - Swift</a></p>
  
  <p><a href="https://stackoverflow.com/questions/42174609/userdefaults-in-ios-10-is-sometimes-showing-old-value/42175292">UserDefaults in IOS 10 is sometimes showing old value</a></p>
  
  <p>When a user login into the app I am storing some values from login API response in user defaults.</p>
  
  <p><code>UserDefaults.standard.set(val, forKey: "XYZ")</code> </p>
  
  <p>When the user log out of the app I'm deleting user defaults.</p>
  
  <pre><code>logOutAlert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action: UIAlertAction!) in
      // Mark:- Function that remove user defaults data
                          self.resetDefaults()
                          BackgroundTask().stopUpdate()
  
                              let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
  
                              self.showAlert(title: Constant.projectTitle, message: "Successfully loged out.")
  
                              let appDel:AppDelegate = UIApplication.shared.delegate as! AppDelegate
  
                              appDel.window?.rootViewController = loginVC
  
                          }))
  
          // Reset userdefaults
          func resetDefaults() {
              //let domain = Bundle.main.bundleIdentifier!
              //UserDefaults.standard.removePersistentDomain(forName: domain)
              UserDefaults.standard.dictionaryRepresentation().keys.forEach(UserDefaults.standard.removeObject(forKey:))
      //        let defaults = UserDefaults.standard
      //        let dictionary = defaults.dictionaryRepresentation()
      //        dictionary.keys.forEach { key in
      //            defaults.removeObject(forKey: key)
      //        }
          }
  </code></pre>
  
  <p>Commented lines are the ways I tried to solve a problem.
  Please go through following scenario</p>
  
  <ul>
  <li>Install app</li>
  <li>Log in (user A)</li>
  <li>Log out (user A)</li>
  <li>Log in (user B)</li>
  <li>Kill app from memory (not uninstall)</li>
  <li>Wait for around 20 mins.</li>
  <li>Relaunch app. </li>
  </ul>
  
  <p>Issue - user A's user defaults data restored automatically.</p>
  
  <p>Thanks in advance. Any help surely appreciated, sorry for my English.</p>