I've come across some odd behavior with regards to classes annotated with the StateObject
property wrapper. The body
computed property of a View
would be invoked prior to the StateObject
being installed. This would result in the following warning in Xcode:
Accessing StateObject's object without being installed on a View. This will create a new instance each time.
Once this occurs for a particular View
, any instance of that view will always render a new state object instance each time body is invoked regardless of wether or not the view is deallocated and re-created (i.e dismissing the screen that is hosting the view and returning to it).
The only time the error no longer occurs is when the app is restarted. This happens intermittently and I have been unable to pinpoint the reason. I've checked the code and I do not invoke the body property manually in any way. It seems to happen behind the scenes during a view's lifecycle
One common characteristic among affected views I noticed is this seems to happen to StateObject
s whose views are nested within a VStack
.
Is this is a known bug? I've been encountering this experience with Xcode 12.5 and below
Any insight would be great to help avoid this bad experience for users.
Best,