SDK Setup Guides
...
Quick Tips for Swift
@StateObject vs. @ObservedObject
views in swiftui can be redrawn frequently therefore, it’s important to understand why and when to use the @stateobject and the @observedobject property wrappers when observing an observed object both property wrappers tell a swiftui view to redraw when the object they are observing updates the difference between them can be seen when a view is redrawn this is a general concept that and not specific to ditto @observedobject https //legacydocs ditto live/ios/quick tips/stateobject vs observedobject#observedobject when a view is redrawn the entire view struct is initialized all over again this means that all of the variables that were created in the view that are not marked with the @state property wrapper also get initialized all over again and set to their default values, including objects marked with @observedobject @stateobject https //legacydocs ditto live/ios/quick tips/stateobject vs observedobject#stateobject when a view is redrawn objects marked with the @stateobject property wrapper do not get re instantiated this means that an object marked with @stateobject , the first instance of this object that we create will persist and be used each time the view is redrawn example https //legacydocs ditto live/ios/quick tips/stateobject vs observedobject#example to better understand the difference between these two property wrappers try running the code below we have two counters, the first is created in the contentview view and the second is created in the countertwoviewmodel class which is being observed by the countertwoview the contentview is calling to the countertwoview to display the second counter on the screen watch what happens to the second counter value when you increment the first counter and note the difference when you change the viewmodel object in the countertwoview to use @stateobject rather than @observedobject struct contentview view { @state var counter = 0 var body some view { vstack { text("counter one is \\(counter)") button("increment counter") { counter += 1 } } padding( bottom) countertwoview() } } struct countertwoview view { // change between @observedobject and @stateobject @observedobject var viewmodel = countertwoviewmodel() var body some view { vstack { text("counter two is \\(viewmodel count)") button("increment counter") { viewmodel incrementcounter() } } } } final class countertwoviewmodel observableobject { @published var count = 0 func incrementcounter() { count += 1 } }s when using @observedobject on the viewmodel you will notice the second counter resets to 0 every time the first counter is incremented this is because the contentview gets redrawn when you increment the first counter causing the countertwoview to be re initialized; which means, the counter in the viewmodel gets re initialized and set to its default value of 0 when using @stateobject on the viewmodel object the viewmodel object does not get re initialized every time the view is redrawn; therefore, our counter does not reset to its default value of 0 avoiding side effects when using ditto https //legacydocs ditto live/ios/quick tips/stateobject vs observedobject#avoiding side effects when using ditto a common issue we see in reactive apps is a failure to dispose of resources as the view is re drawn if you use an observableobject for your ditto live queries, call livequery stop() to cancel them; otherwise, your app may see a large accumulation of publishers that infinitely grow conclusion https //legacydocs ditto live/ios/quick tips/stateobject vs observedobject#conclusion in general, if your view has a high redraw rate or when an observable object is being used in the same view that initialized that object then you should use @stateobject if the observable object is initialized outside of the view that is using it, use @observedobject , and clean up any long running tasks when the view is disposed additional information https //legacydocs ditto live/ios/quick tips/stateobject vs observedobject#additional information https //www donnywals com/whats the difference between stateobject and observedobject/ https //www avanderlee com/swiftui/stateobject observedobject differences/ currently, there's no official documentation on initializing a @stateobject property with parameters; however, consider this example https //swiftui lab com/random lessons/#data 10