I use NSUserDefaults
to persist approximately 100 key/value pairs on the user's iOS device. Each pair is just a string key and boolean value. This works fine almost all the time. Lately, a few users mentioned their app being 'reset'. Specifically, their app was not correctly reading data from NSUserDefaults
. I'm trying to understand how this can happen.
A few things to note:
- I call
synchronize
after every update to the user defaults - I don't have any code that clears individual entries or the entire defaults
- The defaults are read in
application:didFinishLaunchingWithOptions:
- The defaults are not read when the app moves from the background to the foreground
I found some interesting comments in this Loom.com blog post. Sounds like NSUserDefaults
backing plist might be inaccessible when the app is restarted in the background. I'm not sure if backgrounded apps are restarted if they crash while in the background. However, I'm curious because my app is indeed crashing in the background according to my crash reporting service. Also, this crash happens immediately after receiving a memory warning.
Is it possible for the app to fail reading the user defaults when restarted in the background after a crash (while in the background)?
Any advice on how to diagnose this issue is greatly appreciated!
Edit - more info: sounds like the CoreLocation framework can cause apps to restart in the background after crashing in the background. My app includes a few 3rd party advertising and analytics SDKs. In fact, this problem started to show up after the addition of one particular SDK which could use CoreLocation.