Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

FileStorageKey (Default) Creates Empty File Instead of Populating It with Data #167

zachgibson started this conversation in General
Discussion options

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.

Image

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

FileStorageRepro.zip

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
You must be logged in to vote

Replies: 4 comments

Comment options

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!

You must be logged in to vote
0 replies
Comment options

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.

You must be logged in to vote
0 replies
Comment options

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.

You must be logged in to vote
0 replies
Comment options

For now I'm creating the file and seeding the contents myself at creation time. @mbrandonw any considerations here or is this totally fine?

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Converted from issue

This discussion was converted from issue #166 on June 06, 2025 15:06.

AltStyle によって変換されたページ (->オリジナル) /