10

I use [NSUserDefaults standardDefaults] to store a boolean to see if it is the first time that application is being launched ... If so, the app should show a registration window.

  • It was working fine till last week, but now, sometimes when I switch to other apps and come back after a little while, I see that registration page loads while it shouldn't.

  • I used NSLog to see what is stored in [NSUserDefaults standardDefaults] and I see that the values I stored has been set to nil (null) while I haven't done that anywhere in my code.

Does anyone know why the values do reset ?

P.S: Actually the values are not permanently lost , because if I don't do anything in registration page and quit the app instead, It will launch normally the next time I enter the app !!!

Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
Sepehrom
  • 1,335
  • 2
  • 16
  • 33
  • Are you sure you have called `[ [NSUserDefaults standardUserDefaults] synchronize]` ? – Avt May 03 '14 at 11:46
  • Yeah, I have called that ! As I described in my question ! It was working fine for a long time and it doesn't happen every time , so it must be synchronized ! – Sepehrom May 03 '14 at 11:49
  • Your question never mentions the synchronize call. Are you positive you call synchronize after changing a default value? – Duncan C May 03 '14 at 12:27
  • Yeah, I call `synchronize` every time I change a value in `standardUserDefaults` – Sepehrom May 03 '14 at 12:33

4 Answers4

1

A long time ago I encountered this issue, turns out a third party library that I was using uses the same key when storing values to NSUserDefaults. Try searching your project for this key, maybe something else is resetting it.

Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
  • First, I have done that. Found no similar key. And Just to make sure I changed the key. But same problem exist. Even if There's one thing that is resetting that key, one thing has no explanation : Why the value is being restored to the right one when I quit and relaunch ??? – Sepehrom May 03 '14 at 12:59
  • For me it was the case that i had put in a code a while back and had forgotten that it that part erasing my app defaults. – Tanzeel Nov 18 '17 at 00:44
1

Here are the ways I know about to lose values in NSUserDefaults, in order of likelihood:

  1. The key was never saved in the first place
  2. Your app deletes it later on
  3. The app is deleted and reinstalled
  4. Another app overwrites or removes the key
  5. The phone or simulator is reset
  6. During the night, the phone is replaced by an identical-looking, different phone

It sounds like, from the discussion here, that you've ruled out 1,2,4, and probably 3 & 5. The only next debug step I can think of is to store the test phone in a locked drawer at all times.

But I'd leave my money on an intermittent problem causing #1. For that, we'd need posted code to investigate.

EDIT -

A high % of NSUserDefaults problems posted here are about storing BOOLs and other scalar types. It looks like the OP knows about wrapping in NSNumbers, but BOOLS in particular are fraught because it's easy to confuse false-y values like NO no and nil, and NSNull instance.

Let's throw that on the list for this question at #2.5. There again, would need code to confirm.

danh
  • 62,181
  • 10
  • 95
  • 136
1

If this is happening while testing, it's normal. The fact that the program is even making this decision (should I show the registration page?) suggests that the app has been forcibly quit and is starting from scratch. When testing, this can result in clearing out the app sandbox as the app is reloaded from Xcode. In the real life of a real user, however, that won't happen (unless the user deletes the app from the device).

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

Make sure you are calling [[NSUserDefaults standardUserDefaults] synchronize] just after setting preferences and you are not overwriting you preferences.

2intor
  • 1,044
  • 1
  • 8
  • 19
  • Yeah, I have called that ! As I described in my question ! It was working fine for a long time and it doesn't happen every time , so it must be synchronized ! – Sepehrom May 03 '14 at 11:47