jessesquires / Foil

A lightweight property wrapper for UserDefaults done right
https://jessesquires.github.io/Foil/
MIT License
458 stars 26 forks source link

[Bug]: user defaults returns outdated value after rebuilding project #37

Closed JonnyBeeGod closed 2 years ago

JonnyBeeGod commented 2 years ago

Guidelines

Project Version

2.0.0

Platform and OS Version

iOS 15, iOS 13.5

Affected Devices

Tested on various Simulators

Existing Issues

No response

What happened?

When using the example project and updating one of the values like e.g. flagEnabled, after rebuilding the app the flagEnabled value is reset to the old value that has been saved previously before the last change.

Steps to reproduce

  1. Build and run the example project
  2. Toggle the flagEnabled value (can also be reproduced with the other example keys)
  3. rebuild and run the project again
  4. Observe the flag value displays the old value again

Expected behavior

Since those values are backed by UserDefaults, I would expect them to be preserved between app launches. So that when we update a key with a new value, when starting the app again we should see the previously updated value instead of the one from before.

Attachments

No response

Screenshots or Videos

https://user-images.githubusercontent.com/2844335/147389621-c265755a-1367-409a-966e-8ce629d4cf52.mp4

Additional Information

Issue can be seen quite good in the video. This also happens if we safely move the app in the background first and then rebuild the project. Also when stepping in the code via debugger it shows that the issue is not caused by the example UI code, but the flag value returned by AppSettings.shared.flagEnabled is in fact the outdated value.

JonnyBeeGod commented 2 years ago

Nevermind, this is actually a timing issue. Even though UserDefaults has synchronous APIs to update values it takes a couple of seconds (yea ...) for the changes to write through to disc. So this behavior is not reproducible if you wait for ~10 seconds after changing the value and then reopening the app.

jessesquires commented 2 years ago

Thanks @JonnyBeeGod ! 🙌🏼