Objective-C NSUserDefaults caching prevents another app from accurately reading changes

后端 未结 1 1399
灰色年华
灰色年华 2021-01-23 05:13

I have one application that sets preferences. It uses those prefs, and so does another application. When I use the following code in the second application, it reads the prefe

相关标签:
1条回答
  • 2021-01-23 05:47

    You should be able to call +[NSUserDefaults resetStandardUserDefaults] in the second program to force +[NSUserDefaults standardUserDefaults] to create a new instance and reload values from disk the next time it's called. Calling -synchronize in the first program is the correct approach to make sure the updated defaults are written to disk (I'm not sure how NSUserDefaultsController behaves in this regard).

    Note that calling +[NSUserDefaults resetStandardUserDefaults] every time you access the standardUserDefaults may result in a performance penalty if you do so frequently, since it will presumably mean a disk hit on every read.

    0 讨论(0)
提交回复
热议问题