Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UserDefaults is not suitable for data model layer persisting #235

Open
Xedart opened this issue Mar 2, 2020 · 6 comments
Open

UserDefaults is not suitable for data model layer persisting #235

Xedart opened this issue Mar 2, 2020 · 6 comments

Comments

@Xedart
Copy link

Xedart commented Mar 2, 2020

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
Copy link
Owner

💔

@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
Copy link
Author

Xedart commented Mar 2, 2020

@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
Copy link

pedrommcarrasco commented Mar 2, 2020

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 Xedart changed the title The most overrated library I have ever seen. Don't use it. UserDefaults is not suitable for data model layer persisting Mar 2, 2020
@Xedart
Copy link
Author

Xedart commented Mar 2, 2020

@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
Copy link
Owner

@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:

  • it’s too easy to use and so it encourages bad patterns
  • default values behaved unexpectedly last time for you

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
Copy link
Author

Xedart commented Mar 3, 2020

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants