Strange NSUserDefaults behavior

橙三吉。 提交于 2019-12-06 12:18:45

Killing an app from the taskbar when it's suspended just sends it the SIGKILL message; no graceful shutdown happens.

If you're backgrounding and then killing the app, you might not be getting the user defaults synchronized (written to disk) before the app dies, which happens by itself on an internal timer usually, but if you've written to the defaults and then the thing gets suspended before the auto-synchronize happens, that could explain this behavior.

Put this (as you note):

 [[NSUserDefaults standardUserDefaults] synchronize];

in your -applicationDidEnterBackground handler in the app delegate. That way, even if the app dies or is killed while it's in the background, you know the defaults have been written.

As a defensive practice, I generally explicitly call synchronize on the defaults object every time I write state that I consider critically important.

Where are you calling the -synchronize method? Are you paying attention to the app delegate messages sent by the OS that let you know your app is about to go down?

I'm not sure if this is it, but have you called [[NSUserDefaults standardUserDefaults] registerUserDefaults:...]?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!