-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Heap allocation of Storage class when a View using @AppStorageCompat is recreated during a state change in parent View #6
Comments
Interesting idea! I played around with it during development, but found that I didn't need it initially. I'll play around with this on the weekend 👍. |
It's easy done with a StateObject. However I suppose that isn't available in the old OS version you were trying to back port AppStorage to. On the other hand if you did use StateObject then you would have a far superior implementation of AppStorage for the current OS!
|
Maybe there's a way to implement it differently using an |
(By the way, I tested using |
If |
Hi is there any way that you can prevent this heap allocation made every time a View struct is created that declares a @AppStoreCompat?
AppStorage/Sources/AppStorage/AppStorage.swift
Line 11 in db62f9c
_value = Storage(value: value, store: store, key: key, transform: transform)
E.g. in the code below every time the button is tapped to increment the
stateCounter
the body is recomputed which results in ContentView2 being init which is using @AppStoreCompat so causes this heap allocation of a new instance of the Storage class. It seems unnecessary to recreate the Storage class and have it re-register KVO observing when the View hasn't changed since last time. Perhaps there is a way to store the key and use it for equality and then delay creation of the Storage class until DynamicProperty's update call (which I noticed you aren't using).I should say that Apple's implementation has the same behaviour as yours right now, but I'm sure they will fix this soon.
The text was updated successfully, but these errors were encountered: