sunshinejr / SwiftyUserDefaults

Modern Swift API for NSUserDefaults
http://radex.io/swift/nsuserdefaults/static
MIT License
4.85k stars 365 forks source link

UserDefaults is not suitable for data model layer persisting #235

Open Xedart opened 4 years ago

Xedart commented 4 years ago

When I saw SwiftyUserDefaults I thought it's a really nice little lib that adds some nice syntactic sugar over UserDefaults. As a result I used it extensively in my project, and because it allowed easy storing of custom structs in UserDefaults I used it for my model layer. The result is that I had to deal with tons of bugs in my model layer because of these issues: Data is not stored sometimes. Values set by default behave strange Data is not updated sometimes. Data is not stored immediately. Infinite compilation (sometimes)

So, if you are just like me, and want to use it as a solution for persisting your model layer, look for something different if you don't want to get a lot of pain.

sunshinejr commented 4 years ago

💔

@Xedart @zhuravel-sergey I'm sorry that you feel this way about the lib and hope you find something else that works better for you. I just wanted to clarify few things:

Data is not stored sometimes. Data is not updated sometimes. Data is not stored immediately.

I doubt these are related specifically to this library, since it looks like synchronization problems, but I would love to investigate it further if given some demo code/project.

Values set by default behave strange

Can you provide more details? Default values are defined as values that are returned when there is no other value saved to the key.

Infinite compilation (sometimes)

There was one bug in Swift toolchain and we both reported it on Swift JIRA and also added a workaround so it doesn't happen anymore. This was also in beta version, btw. However, if you still experience any problems with it please let us know.

Xedart commented 4 years ago

@sunshinejr I experienced infinite compilation when I accidentally mistyped the name of my Defaults-stored property. In that case, instead of receiving a compilation error, I received infinite compilation. And it was not obvious what's wrong and took a lot of time to figure it out. I assume the blame for this is mostly on Swift compiler rather than the lib, but nevertheless it occurred while using SwiftyUserDefaults.

As for default value, I faced an issue when my code behaved differently in case when I saved my default value to Defaults in regular way on app start rather than using default value syntax.

And for Data is not stored sometimes. Data is not updated sometimes. Data is not stored immediately.

I agree that this is related to how UserDefaults works rather than how SwiftyUserDefaults works, but I wanted to warn people that using UserDefaults (and SwiftyUserDefaults) for persisting model layer is a bad idea.

pedrommcarrasco commented 4 years ago

You could have said it in other words without looking so offensive. After all this a free open-source library, if there's a problem you can always try to fix it instead of blaming the authors (or the library itself).

I've never used SwiftyUserDefaults but @sunshinejr is working on this library during his free (& probably unpaid) time so the least someone could do is show some respect and provide actual feedback to the framework.

Just my two cents, Stay safe ✌️

Xedart commented 4 years ago

@pedrommcarrasco , @sunshinejr I respect open source software and your work and didn't mention to offence someone. Still, there are some flaws in this lib. Maybe I was too emotional with my initial title and wasn't specific in describing the issues, I will try to provide more actual feedback as I will have some free time.

sunshinejr commented 4 years ago

@Xedart yeah, usually you don’t really want it as a persistance layer since User Defaults are just plists on the disk and stored in memory for the app usage. It means that the more/bigger objects you have, the more problems you will run into. E.g. you will have a lot more memory used by the app, you will be reading/writing a lot more data and doing it a lot more often (which will make a lot more locks and so the updates/saving will not happen immediately). And it goes without saying but when you see User Defaults as a plist-on memory storage, you also can notice that the security of your models is at risk.

So it seems like there are two problems with the lib in that case:

While the first one is more kinda the whole purpose of this lib (to be really easy to use), I would love to help you out with the default value problem. So please create a new issue once you have some time to dig more.

Also let me know if I can help you with anything else 👍🏻

Xedart commented 4 years ago

@sunshinejr As for default value problem I figured it out now. The issue was in my misunderstanding of how the feature actually works. In my case, I stored and array which had to be prepopulated with one initial element at the app launch. After that user can add additional elements to the array. The problem occurred due to the fact that after user adds new element, this new element actually becomes the only element of the array, because default element is not there anymore. Now I see that this is how the default value is intended to be by design, and I misunderstood their semantic. It doesn't make any difference if your store simple values like Int or String, but there is difference when it comes to storing an array. Maybe you could clarify this particular issue in the docs somewhere.