This is intended to be a theoretical question, not help with coding. The coding I can handle. I have a bunch of user options/prefs that are set in a settings window & not changed anywhere else. @AppStorage does a fine job of persisting the values & sharing them with the views that need them, working similarly to @State. However, after much reading I am given to believe that SwiftUI recreates (redraws?) views frequently. Does this mean that @AppStorage is reading the preference values from my hard drive frequently? 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? I have searched everywhere (I think) & have found no clues. As these values are user options, I don't anticipate that they will be changed all that often. I don't want to use the app environment if that is just creating an unnecessary duplicate of the values. But, if it is more efficient than multiple trips to the hard drive, maybe I should. 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? Is this even really an issue?
I will share code if anyone thinks it is necessary. Thank you for any thoughts or suggestions you might offer.
This is intended to be a theoretical question, not help with coding. The coding I can handle. I have a bunch of user options/prefs that are set in a settings window & not changed anywhere else. @AppStorage does a fine job of persisting the values & sharing them with the views that need them, working similarly to @State. However, after much reading I am given to believe that SwiftUI recreates (redraws?) views frequently. Does this mean that @AppStorage is reading the preference values from my hard drive frequently? 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? I have searched everywhere (I think) & have found no clues. As these values are user options, I don't anticipate that they will be changed all that often. I don't want to use the app environment if that is just creating an unnecessary duplicate of the values. But, if it is more efficient than multiple trips to the hard drive, maybe I should. 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? Is this even really an issue?
I will share code if anyone thinks it is necessary. Thank you for any thoughts or suggestions you might offer.
Share Improve this question asked Mar 10 at 13:18 rbkopelmanrbkopelman 1271 silver badge7 bronze badges 1- 1 SwiftUI doesn't draw anything it just diffs lightweight View structs and only if there is a difference then it goes about configuring UIKit/AppKit objects and those do the actual drawing. – malhal Commented Mar 10 at 17:41
1 Answer
Reset to default 0How 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.