App Groups
UserDefaults not clearing datas immediately using suite name: App Group

UserDefaults not clearing datas immediately using suite name: App Group

I created this post when I got and issue on trying to clear UserDefault values. Here is my problem scenario.

  1. Trying to save if user is logged in or not in user defaults.
  2. I used a code to save user login status with following code
    • UserDefaults(suiteName: "group.santosh.loginApp")?.setValue(true, forKey: "group.santosh.logginStatus")
    • It successfully stored the value.
  3. Here is a code to clear all user defaults values.
    • UserDefaults().removePersistentDomain(forName:"group.santosh.loginApp")
    • This also clears successfully.

So, that is the problem?

I tried to redirect from dashboard to login view controller once the user is logged out. I sent a server request to make user logged out. Server sent me a successful response and I tried to clear all user default values stored locally. But I was completely unable to clear the values.

Success:

  • Login from loginVC
  • On dasboardVC, tried to logout, it does clear user defaults value successfully and redirects to login vc.
    • Please note, I have written code which will redirect to corresponding VC based on login status stored in user default.
  • This process was a successful store and clear for user defaults values. And I did this without need to close or anything additional work.

 

Failure:

This is the state when user is logged in and closes the app. Now user opens the app, they will be at dashboard VC.

  • Now at dashboardVC, if user tries to click logout button, it doesn’t clear user defaults values.
  • I closed the app and open the app, it shows LoginVC.

 

Attempts:

  • I searched with the whole code if I have done something wrong. But then I realized that the very same code was working which I have explained in Success section above.
  • I tried if this was because of running code in background thread or in main thread. I tried everything but the problem was still the same.
  • I tried to do exactly the same with same code in new project and luckily it was working in new project.

 

What I did then?

Well, I tried changing App Group name to new one. And voila, it worked. Haha ?. Oh my God.

Don’t forget to delete old entitlements and update the App Groups name in your code as well.

Anyway guys, I though this might be helpful to someone, so, I decided to post it here.

Thanks for reading my post.

Happy Coding.

???

 

Leave a Reply