10

When the user logs in to my application, I download a token from my JSON server and store it in NSUserDefaults, as such:

[[NSUserDefaults standardUserDefaults] setValue:token forKey:TOKEN];

When the user logs out in a subsequent page, I call this method and return back to the login screen:

[[NSUserDefaults standardUserDefaults] setObject:@"" forKey:TOKEN];

(and before that I called [[NSUserDefaults standardUserDefaults] removeObjectForKey:TOKEN];)

It doesn't matter how I try to delete this user defaults, whenever I load up my app, it always shows me the full token and not an empty string nor a null value.

When reading around, apparently it has something to do with read write cycles? But even if i leave it for a while, the key still remains. Is this a simulator problem?

Whatever the cause, how do i get around this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Rambatino
  • 4,716
  • 1
  • 33
  • 56
  • Could you elaborate more on how you load the app? I tried to log the value before and after removeObjectForKey and it works fine: `[[NSUserDefaults standardUserDefaults] setValue:@"test" forKey:@"token"]; NSLog(@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"token"]); [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"token"]; NSLog(@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"token"]);` – Valent Richie May 20 '13 at 00:41
  • 1
    When i logged it like that, immediately before and then immediately after, it goes from existing, to then being deleted. However, if i access that data after i've closed down my app and reloaded it, the string appears again! (and after a while it deletes it and now it won't save it again) – Rambatino May 20 '13 at 00:49
  • It only loads into a view controller, so I don't really understand where the problem lies... – Rambatino May 20 '13 at 00:53
  • When you said you reloaded the app, do you mean running the project from xcode again? If you re-run the project right after some modification in `[NSUserDefaults standardUserDefaults]`, it might still retain the old value in the re-run. – Valent Richie May 20 '13 at 00:56
  • Yer it seems that is the case. Will it happen on a device? I don't have one at hand at the moment - as in, if the user logs off and then closes the app and quickly reopens it, will the nsuserdefault object still be there? – Rambatino May 20 '13 at 01:04
  • 1
    It should not happen on the device. Take a look at the following question and its answer: http://stackoverflow.com/questions/2622754/why-is-nsuserdefaults-not-saving-my-values – Valent Richie May 20 '13 at 01:23
  • I'm facing this issue on device iPhone 6S – Ricardo Mar 15 '17 at 09:29

2 Answers2

22

It's the simulator problem of caching the memory first. It only happens in xcode and should not happen on a device.

Rambatino
  • 4,716
  • 1
  • 33
  • 56
  • 1
    It was the simulator in my case that was causing the issue. – Robert J. Clegg Apr 02 '14 at 20:17
  • 10
    Until you are actually writing unit tests, and those can't be run on a device. How are we suppose to know the tests actually succeed? – Mazyod Nov 06 '15 at 05:32
  • 3
    Has anyone figured out how to fix this so we can properly reset Unit tests? – Kudit Sep 28 '16 at 03:22
  • 6
    I was almost going mad because of this bug. My tests keep failing and I thought it may be a threading problem and I was questioning already my existence and my skills as such. Almost changed to a career as toilet cleaner. What can we do?! – ben Feb 16 '17 at 13:09
7

Do you call

[[NSUserDefaults standardUserDefaults] synchronize];

after the key was deleted? Maybe you are not persisting the changes into the database.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Ernest Collection
  • 2,024
  • 1
  • 13
  • 6
  • Although this isn't a problem anymore, that was the method. I really should read the documentation more often. Thanks for that, I'll use it in future. I've made yours the correct answer – Rambatino Sep 12 '13 at 07:49