4
votes

I am facing a very weird behavior for NSUserDefaults, the problem is that [NSUserDefaults standardUserDefaults] objects are being removed randomly!

My [NSUserDefaults standardUserDefaults] contains around 65 objects(60 small NSStrings and 3 arrays which the maximum count could be 4 and 2 other arrays with maximum count 30..notice that it has never been the maximum case when facing this problem) , one of these objects is a value checking if the user has already completed the registration phase. When launching the application, sometimes this NSUserDefaults will contain only 5 objects from those 65 and the others are being removed from the plist without appearing again even if i relaunch the app., which lead the user to the registration phase again!!

i am pretty sure that i am using the save function correctly

[[NSUserDefaults standardUserDefaults] setObject:@"Value" forKey:@"Key"]; [[NSUserDefaults standardUserDefaults] synchronize];

i have searched google for similar behavior without finding anything that can help! Does anyone faced such behavior and what is the solution to fix it?

Thank you for any help

1
did you try setObject instead of setValue ? do you have duplicate keys ? values ? - Lena Bru
Sorry i updated my post..i am using setObject in all my project and not setValue..i tried setValue in some but still without success. What do you mean by duplicate keys? whenever my values is being updated then i save it again under the same key name! - Iphone User
when checking the plist in normal behavior , all the fields are saved successfully without any duplicates - Iphone User
can you print out the contents of nsuserdefaults after every key save ? - Lena Bru
They are all saved successfully because these values are being used inside the application by reading them from NSUserDefault and displayed normally..but when i exit the application and reopen it after some time, all these values are being removed (randomly and not always)..Also, the weird thing is that objects related to the registration phase are not being changed when user open the app and already did the registration before, so why these values are being removed? i checked that no limitations on saving NSUserDefault as long as user have space..so this error is driving me crazy :S - Iphone User

1 Answers

4
votes

I do want to help out here, since I have had exact same strange behavior with one of my projects.

So bear with me, what happened to my project is that: I had a singleton class, which encapsulates several properties, and I had overridden setters and getters for those properties. In a setter method, I get standardUserDefaults instance and set object for key, and synchronize. In a getter method, I return the object of the key. Also I have a login success indicator value to indicate if login is successful. And same as your issue, my objects disappear. After few days of struggle, it turns out that the login indicator got initialized to false when Network became unreachable. And in the indicator false clause, I was setting nil objects to user defaults.

My points are:

  • Double check if standardUserDefaults setObject:forKey: function calls are logically correct, make sure the objects are NOT nil when call
  • Check if you have logic (login, Network change etc) that invalidates the objects.
  • Run tests, find the minimal steps to replicate the issue. Use Debug session to examine the objects. (Give up believing in Random Disappearing)

Hope this gives a lead.