NSUserDefaults values lost on background launch

There have been many threads and bugs about this, but it's happening to me again in ios 9. I have an app that launches in the background in response to NSURLSession tasks and content-available pushes. Reproducibly, if I reboot my phone and wait for a background launch of my app to happen, then when I open the app I find that [[NSUserDefaults standardUserDefaults] dictionaryRepresentation] contains all the system values, e.g. AppleITunesStoreItemKinds, etc. but does not contain any of the values I have set. If I force-quit and relaunch the app all of my values come back. Is there any way to avoid it caching the "empty" standardUserDefaults from before the phone is unlocked, or at least to determine when they are messed up and fix them without having to force-quit the app?


Thanks,

eric


previous discussions:

https://devforums.apple.com/thread/212999

http://www.openradar.me/16761393

Replies

I have the same problem on iOS11. After having rebooted iOS device and launch the app all data in NSUserDefaults are not available.

The problem doesn't raise if I remove the request of passcode to access to iOS device. Until now I didn't find any solution.

I also running into this issue. We have a voip app. App wakes up at background before first unlock. And NSUserDefaults is stay bad states until phone unlock and app restart.


I am trying to find a replacement for NSUserDefaults. I wonder if core data is a good choice, I haven't try core data yet.


Does anyone has a solution yet for NSUserDefaults replacement?

Core Data sounds like overkill. I'd use a plist or json file—with appropriate permissions—to store the data.