If you really want to use NSUserDefaults to store your data…

Hi all, this post is going to talk about what you should do when using NSUserDefaults store data in iOS development.

As Apple suggested we should only use NSUserDefaults to store user setting: such as true or false flag. When it come to larger data persistence, a proper database should be a better choice than `UserDefaults`. However, UserDefaults has lots of fans due to its user-friendly API and similar behavior to Dictionary.

When you are using UserDefaults, you may easily write down these codes:

We want to have only one copy of the data in app, that’s why we use the Manager.datasdirectly . But this causes some performance issues, what’s the solution?

We keeps the datas in memory , every time we modify it we do a sync operation. Here goes the solution:

Remeber this only works when your data is not that big, and you don’t want to use database for only ten records. Otherwise, don’t hesitate to use a database.

Hope you enjoy.