@Published from a non-main thread in Xcode 12.5b2

I just wanted to raise some awareness to a change in behaviour in Xcode 12.5b2.

I noticed SwiftUI views in an app I work on had completely stopped working, because our view model (which is an ObservableObject) was no longer triggering view updates when its @Published properties changed.

After much investigation we discovered that we were accidentally changing one of our @Published properties from a background queue. Now here's the weird thing: I caught this because the latest beta was giving me one of those nice purple runtime warnings:

Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.

Now I'd expect this to not work, but what I wouldn't expect is for it to completely break the ObservableObject system such that all other @Published property changes (which happen on the main queue) no longer trigger updates - presumably something about trying to change one of them on the background queue breaks the whole thing.

This was happening from a NWPathMonitor callback. I've not seen this warning in previous Xcode versions, which raises the questions:

  • Is this warning new in Xcode 12.5?
  • If not, has NWPathMonitor's behaviour changed and it used to actually call back on the main queue and now it does not? Seems unlikely.
  • Was there simply a bug in previous Xcode versions which prevented us from seeing this warning?
  • If we were changing this @Published property on a background queue before, why did it otherwise continue to work but now its completely broken?

The fix of course was to simply make sure we dispatch back on to the main queue in our network path monitor callback, but this is something to watch out for because any unintentional changes to @Published properties on a background queue now seemingly breaks in a pretty terrible way.

Terms of Service

Privacy Policy

Cookie Policy