0

I am testing something on my app where I save the current used theme as a string: "Light" vs "Dark".

I just realized that at some point when I keep switching from light to dark (reading and writing the string to UserDefaults) I sometimes get on old value:

Light -> Dark -> Light -> Dark -> Light -> Light

I have added logs to where I read and write the value and its doing it correctly. This is how I test:

 1. Start app 
 2. Change theme (save string to UserDefaults)
 3. Stop app
 4. Start app (read string from UserDefaults)

I saw some other posts which says there is a problem with UserDefaults in IOS 10 and the solution is to restart device or mac.

I get this, but doesn't this mean the issue will appear on the user device as well ? Has anyone encounter this kind of issue ? Any solution ?

I am using Xcode 8.2.1 with IOS 10, Swift 3. And I am testing on my real device, not simulator.

Adrian
  • 19,440
  • 34
  • 112
  • 219

1 Answers1

3

Are you sure that you call [[NSUserDefaults standardUserDefaults] synchronize]; after saving Data?

Chris8447
  • 306
  • 4
  • 7