Iphone User
Iphone User

Reputation: 1950

NSUserDefaults standardUserDefaults objects are being removed randomly

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

Upvotes: 4

Views: 1067

Answers (1)

vladof81
vladof81

Reputation: 26499

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.

Upvotes: 4

Related Questions