And what if you decide to publish a variable that wasn't before, and now you have to run around checking every potential change to it? Not to mention unmaintainable, since I or some future maintainer may be calling a function a level or two or three above where a published variable is actually set. So the go-to workaround appears to be wrapping every potentially problematic setting of a variable in a call to DispatchQueue.main. This just creates a cascade of errors in my application but also (and I may not be interpreting the documentation correctly) I don't want the owning object to do everything on the main thread. Then why is it done? Why aren't the changes simply published on the main thread automatically?īecause it isn't, people suggest a bunch of workarounds, like making the enclosing object a MainActor. Publishing changes from background threads is not allowed This would appear to be an extremely common scenario at this point, and indeed it's exactly the one posed in question after question you find online about this resulting error: Like many, I have run up against the problem where you perform an asynchronous task (like fetching data from the network) and store the result in a published variable in an observed object. Given that SwiftUI and modern programming idioms promote asynchronous activity, and observing a data model and reacting to changes, I wonder why it's so cumbersome in Swift at this point.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |