iOS NSUserDefaults access before synchronize completion

后端 未结 2 976
自闭症患者
自闭症患者 2020-12-12 05:21

If I set an NSUserDefault object and try to access it before it has been synchronized, will I be able to access the object I just added?

<
2条回答
  •  执笔经年
    2020-12-12 05:42

    The purpose of [NSUserDefault synchronize]; is to make the user defaults get written on disk immediately.

    Regarding this:

    I'm unsure of if the synchronization is happening without me knowing it.

    You don't need to call [NSUserDefault synchronize] explicitly to store the value, iOS already does it at appropriate time (like when going into background). So you can avoid that line unless you want to store that value immediately. In fact, it's a performance problem if you call synchronize every time you set a value.

提交回复
热议问题