ios – “Publishing changes from background threads is not allowed” after launching a browser and returning to the app


I have a SwiftUI (document-based) app using Swift Data, running on an iPhone 13 with iOS 17.2. In one of the Views, I have

@Environment(\.openURL) private var openURL

var body: some View {
  VStack {
      Button("Open Browser") {
        openURL(URL(string: "https://some.url")!)
      }
  }
  .navigationTitle("Settings")
}

When clicking the button, the browser launches normally. However, when returning to the app using the “back” navigation link on the top left, the app first logs

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.

Then, in a collection getter of one the Swift Data @Model-s, supposed to get a collection that is read when the app’s GUI is rendered, the application crashes, saying

Thread 1: Fatal error: Unable to get value - no backing Managed Object

The whole thing looks to me as if, when switching back from the browser to my app, the entire GUI were rendered from a non-main thread (which, as this thread also has no ModelContainer und ModelContext, cannot load the SwiftData models).

Unfortunately, the call stack of this Fatal Error above is not of much help – the generated SwiftData getter is the only Swift code I see there, up the stack are just locations within the compiled assembly code.

P.s. If I just manually put my app into the background, start doing something else and then return, the app works fine.

Why is this happening?

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img