Improve Managing model data article
Originator: | indiekiduk | ||
Number: | rdar://FB16504380 | Date Originated: | 2025-02-15 |
Status: | Resolved: | No | |
Product: | Documentation | Product Version: | |
Classification: | Suggestion | Reproducible: |
What does the documentation issue you are reporting involve? Sample Code Please provide the URL of the content you are reporting an issue with: https://developer.apple.com/documentation/swiftui/managing-model-data-in-your-app I would like to suggest that this article be fixed because it is causing a lot of controversy in the developer community with regard to using @State which is designed for value types with objects. This article is a prime example of common mistakes so if it was changed then that would help the community. I know that when @Observable was first released in 17b1, @State was changed to use an auto-closure making it suitable for objects but that change was rolled back around 17b5 and it seems the documentation has not been rolled back. The fact is @State with non optional class type causes heap memory leaks. There are 2 kinds of leak. The first is when the View is removed its objects stored in non-optional @State stay allocated forever, probably because it is not possible to be set to nil. Also, since @State inits its inital value every time the View is init it causes needless heap allocations of the default version of teh objects. This problem is compounded if the object init inits other objects, or worse starts background work. So now is the time to fix this document and remove all mentions of @State. In the 3 code snippets that have this: struct LibraryView: View { @State private var books = [Book(), Book(), Book()] I would recommend changing it to struct LibraryView: View { let books = Library.shared.books In the "Create the source of truth for model data" section, just remove the first paragraph, the code snippet and the second paragraph. It simply is not correct to store a model object type in @State which is for view data. The section that contains "You can also create a state object in your top-level App instance or in one of your app’s Scene instances. For example, the following code creates an instance of Library in the app’s top-level structure:" needs more major fixes. The text could be changed to: To share an instance of the Library with all of the Views in your app it requires a singleton and also conformation to ObservableObject protocol so it can be used with .environmentObject. And the sample should be updated to: extension Library: ObservableObject {} @main struct BookReaderApp: App { var body: some Scene { WindowGroup { LibraryView() .environmentObject(Library.shared) } } } Please make this a priority because it really concerns me about the amount of memory leaking out in the wild if people continue to follow this invalid article on using model objects with @State.
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at feedbackassistant.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!