Michael Long
Jul 17, 2023

--

The ObservedObject in your view should probably be a StateObject in most cases. ObservedObject's assume that someone else is maintaining a hard reference to the model such that it doesn't lose state should the parent view update.

See: https://betterprogramming.pub/swiftui-the-unsafeobservedobject-quiz-467bb8554262

Secondarily, there's no need to annotate the class with MainActor as it performs no async actions. Further, it's better to only mark specific functions as MainActor rather than the entire class.

https://betterprogramming.pub/async-await-and-mainactor-strategies-cc35b6c58b52

--

--

Michael Long
Michael Long

Written by Michael Long

I write about Apple, Swift, and SwiftUI in particular, and technology in general. I'm also a Lead iOS Engineer at InRhythm, a modern digital consulting firm.

Responses (1)