Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUserDefaults won't delete object for key

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?

like image 523
Rambatino Avatar asked May 20 '13 00:05

Rambatino


2 Answers

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

like image 125
Rambatino Avatar answered Oct 13 '22 17:10

Rambatino


Do you call

[[NSUserDefaults standardUserDefaults] synchronize];

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

like image 40
Ernest Collection Avatar answered Oct 13 '22 17:10

Ernest Collection