Search code examples
iosxcodesimulatoruserdefaults

UserDefaults - not saving consistently when using Xcode Simulator


I am learning iOS development and I have stumbled across an issue where UserDefaults doesn't seem to be consistently saving correctly.

When I open the app, it checks to see if a UserDefaults property exists, if so, it does something. Now, if I save the UserDefaults property and 'synchronize', then press stop on the simulator and then press 'play' again to re-open it, sometimes the UserDefaults value is nil.

I have yet to test this on a physical device, but has anyone had the same issue - if so, is it expected?


Solution

  • Maybe UserDefaults work fine but when you save some data to UserDefaults and rebuild or run use Command + R if already running simulator then sometimes nothing changed (saved data disappear)

    so everytime when this situation(have to save some data to UserDefaults , CoreData or Realm) I stop and exit simulator first and rebuild or run Xcode

    it works fine to me