iOS - setObject vs. Synchronize

筅森魡賤 提交于 2019-12-01 18:34:14

On iOS 7 and earlier:

When you call -setObject:forKey:, NSUserDefaults schedules a synchronize operation itself for roughly 10 seconds in the future. When that occurs, everything up to that point is saved to persistent storage. The idea here is that you can freely use the set and get methods without paying the significant performance cost of going out to disk every time.

On iOS 8, NSUserDefaults works differently, and the delay is a few milliseconds, rather than 10 seconds, so -synchronize is almost never useful.

On iOS 12, all remaining uses of -synchronize are unnecessary. Note that there may still be cases apps want to call it because they support iOS 11 and earlier.

You use synchronize, to update the database, immediately. If you don't use Synchronize, IOS will do it for you but only periodically. It is a best practice to use synchronize every time you make any change, so that if your app crashes all the changes user made will not be lost.

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