-
Notifications
You must be signed in to change notification settings - Fork 59
FileStorageKey (Default) Creates Empty File Instead of Populating It with Data #167
-
Description
When using FileStorageKey<String>.Default
, I’m seeing an issue where the file is created on disk with no contents, even though a default value is provided and used in memory. If I update that state the file then gets populated correctly.
Checklist
- I have determined whether this bug is also reproducible in a vanilla SwiftUI project.
- If possible, I've reproduced the issue using the
main
branch of this package. - This issue hasn't been addressed in an existing GitHub issue or discussion.
Expected behavior
FileStorageKey<T>.Default
with a non-nil default value creates a file on disk with the contents of default value.
Actual behavior
FileStorageKey<T>.Default
with a non-nil default value creates an empty file on disk.
Reproducing project
Sharing version information
2.5.2
Destination operating system
macOS 15.3.1
Xcode version information
16.2
Swift Compiler version information
swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600030.1) Target: arm64-apple-macosx15.0
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 4 comments
-
Hi @zachgibson, this is currently by design. We do not eagerly write the initial value to the external system (in any strategy, including appStorage
). The reason a file with empty bytes is being written is because we need a file on disk to observe changes to it via dispatch source.
Can you describe what problem this is causing for you so that we can evaluate?
But since this isn't an issue with the library I am going to convert it to a discussion. Let's continue the conversation over there!
Beta Was this translation helpful? Give feedback.
All reactions
-
I see @mbrandonw, this makes sense. I guess I'm just using it in an unexpected way.
My use-case is a Metal shader "REPL-like" app. When saving your shader externally Sharing picks that up and we re-compile/draw the new shader. So, when someone creates a new shader in the app I default the string to a sample shader program that has all the necessary boilerplate. This is where I am needing the file to have the contents initially.
Beta Was this translation helpful? Give feedback.
All reactions
-
I'm down to handle this in my own way but I haven't thought of a reliable mechanism to know when to update the state to cause it to be written to the file.
Beta Was this translation helpful? Give feedback.
All reactions
-
For now I'm creating the file and seeding the contents myself at creation time. @mbrandonw any considerations here or is this totally fine?
Beta Was this translation helpful? Give feedback.