0

Just wondering if anybody experience this issue?

I am developing an application in iOS using Objective-C at the moment. Sometimes my data in NSUserDefaults will be missing after I compile the app.

But if I ignore it and recompile the app again the data suddenly reappears.

I already synchronized in several places (not in every key, but only in several places).

If anyone happened to face this issue before I hope you can share how to handle this issue.

P.S. I need a storage to save 1 particular object so I can retrieve it when the app reopens.

Edited to add the code

NSString *enPIN = [[NSString alloc]initWithString:[NSString stringWithFormat:@"%@", [enterField.text md5]]];

[[NSUserDefaults standardUserDefaults] setObject:enPIN forKey:@"pin"];
[[NSUserDefaults standardUserDefaults]synchronize];

NSLog(@"check pin %@", [[NSUserDefaults standardUserDefaults] objectForKey:@"pin"]);

The object is a string, i hash it using md5 and then store it in nsuserdefault, if it only randomly dissapearing maybe its not weird, but its also reapearing again after it dissapear if i recompile the apps

  • Store Objects as given in below answer link to read and store in same state. http://stackoverflow.com/questions/2315948/how-to-store-custom-objects-in-nsuserdefaults – codelover Apr 08 '17 at 16:33
  • Please show your code. Are you testing on a device or a simulator? If a simulator are you always using the same simulator? `synchronize` Ian not necessary. – Paulw11 Apr 08 '17 at 20:58
  • @Paulw11 hi paul i edited my question to add the code, im not using a simulator, im using a device, ipad mini2, – Eka Praditya Giovanni Kariim Apr 09 '17 at 04:36
  • Does your `md5` extension return an `NSString`? Why the whole dance with `initString` & `stringWithFormat`? Why not simply `NSString *enPIN = [enterField.text md5]` ? When retrieving use `stringForKey:` and there is no need to call `synchronize` - get rid of that. – Paulw11 Apr 09 '17 at 06:38
  • yeah, as i see again it actually redundant in my side because previous code, as for the synchronize i googling in here and there, that it needed to make sure, but the issue still happened randomly the issue is already appearing before i put the synchronize, and the synchronize is not giving any effect. – Eka Praditya Giovanni Kariim Apr 09 '17 at 06:53

2 Answers2

0

Check if your defaults are using only string value or bool or such.

If you are using any Object with Class (key and parameters) like NSObject to store in defaults I prefer you do the encoding and decoding accordingly before storing and retrieving the values.

Also If you storing any NSDictionary check if any of the object value inside that dictionary is not anything other than Bool, String , if there also any NSObject class or reference is stored then you may face same issue.

Refer this stackoverflow link as to how encode objects before storing to NSUserDefaults.

Lastly [defaults synchronise] call mandatory on viewwilldisappear or immediately after storing new value whichever way is your implementation.

Hope this helps.

Community
  • 1
  • 1
codelover
  • 1,113
  • 10
  • 28
  • You do not need to call `synchronize` – Paulw11 Apr 08 '17 at 20:59
  • hi, i edited my question to add my code, i only put a hashed md5 string in it, and synchronized it, the weird thing is, after it disappearing, i compile it again, and the value will be reappearing correctly. – Eka Praditya Giovanni Kariim Apr 09 '17 at 04:41
  • Can you do the follow, NSString *enPIN = [enterField.text md5]; [[NSUserDefaults standardUserDefaults] setValue:enPIN forKey:@"pin"]; [[NSUserDefaults standardUserDefaults] synchronize]; NSLog(@"check pin %@", [[NSUserDefaults standardUserDefaults] valueForKey:@"pin"]); Put the logs in Didfinishlaunchwith Option to read and Appwill terminate function and check the value is stored at time of exit and entering of app. – codelover Apr 09 '17 at 04:55
0

I had a similar issue the other day with NSUserDefaults

Not quite sure what was causing it, but it was due to a bug in Xcode. I was able to fix the issue without changing my code at all. I simply cleaned the project (CMD-Shift-K) and restarted my computer, and then it worked just fine. It's worth a try

Are you getting any kind of error messages in the console?

tpatiern
  • 415
  • 4
  • 13
  • i didnt get any kind of error message in the console, its just successfully saved to NSUserDefaults, and sometime randomly disappear, and reappear if i just recompile it again after it disappear. i log the value while tha apps is open, and its already disappear – Eka Praditya Giovanni Kariim Apr 09 '17 at 04:38