79498243

Date: 2025-03-10 14:10:31
Score: 3.5
Natty:
Report link

How does @AppStorage store values on a Mac platform?

Well, if you look up "AppStorage" in Apple's documentation the very first thing it says is:

A property wrapper type that reflects a value from UserDefaults and invalidates a view on a change in value in that user default.

So @AppStorage saves any values so tagged in UserDefaults.

However, after much reading I am given to believe that SwiftUI recreates (redraws?) views frequently.

Yes, but a "view" in SwiftUI isn't the same as a view in AppKit or UIKit -- it's a tiny structure that can be created with very little work.

Does this mean that @AppStorage is reading the preference values from my hard drive frequently?

Probably not. UserDefaults works like a persistent dictionary, but that doesn't mean that it either reads or writes from/to disk every time you access it. You shouldn't assume anything more than what the documentation for that class tells you, but in the past you could call the synchronize() to ensure that any updates were written out, which suggests that UserDefaults does some smart caching of data to reduce disk access. (The docs now say not to call synchronize(), so don't.)

If this is the case, it seems that I should store a copy of the preferences locally, maybe in the app environment as well. Does @AppStorage keep any sort of local copy while the app is running or is it strictly reading form disk?

You are vastly overthinking this. Does your app have a performance problem that you can trace to UserDefaults or AppStorage? If no, stop worrying about it. And if you think you have such a performance problem, be sure to verify that via Instruments or other profiling.

As these values are user options, I don't anticipate that they will be changed all that often.

Then what are you worrying about?

Does anyone have any idea if @AppStorage is disk only storage & retrieval or if there is some local copy lurking around as I run the app?

I have an idea that it's not "disk only" in the sense that even a small amount of profiling will show you that UserDefaults is fast and efficient. Also, it's a class that practically every application on any of Apple's several platforms uses, and one that has been around since the late 1980's (as part of NextStep), so it's something that you can rely on.

Is this even really an issue?

No. Stop worrying.

Reasons:
  • RegEx Blacklisted phrase (3): Does anyone have any idea
  • RegEx Blacklisted phrase (0.5): any updates
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • User mentioned (1): @AppStorage
  • User mentioned (0): @AppStorage
  • User mentioned (0): @AppStorage
  • User mentioned (0): @AppStorage
  • Starts with a question (0.5): How do
  • High reputation (-2):
Posted by: Caleb