EnvironmentObject fails to be found, randomly (on mac os x)

Is anyone aware of the following bug on mac os x regarding EnvironmentObjects?

  1. I properly inject the environment object at the top of my view hiearchy. However, this is while creating a new window, so the code looks something like this:

      let window = NSWindow(...)
      let dch = DocumentCanvasHolder()
      let contentView = ContentView().environmentObject(dch)
      window.contentView = NSHostingView(rootView: contentView)

This causes a new window to appear on the screen, with the content. Most of the time, this works.

But sometimes, after doing this, while the ContentView() view is putting together its children, one of the children views (always the same one in fact) will crash with the dreaded:

Fatal error: No ObservableObject of type DocumentCanvasHolder found. A View.environmentObject(_:) for DocumentCanvasHolder may be missing

Now, I know I'm doing it right, because 90% of the time, I don't get this, and the view trying to reference an @EnvironmentObject of type DocumentCanvasHolder is found. But about 10% of the time, this just fails. No pattern to it.

Is there some defect with EnvironmentObject's on mac os x that anybody is aware of?

I found the pattern at least. Creating a new window always works until...

... i create a popover, dismiss it, and then create a new window. At that point, the environmentObject object is not found.

I'd known that modal windows (e.g. popovers) don't get the same EnvironmentObject treatment as everything else, but here we see that merely the act of creating a popover subsequently breaks EnvironmentObjects even in contexts where they formerly were working OK.


Terms of Service

Privacy Policy

Cookie Policy