environmentobject access

I've written this app with SwiftUI for tvOS and is trying to add an EnvironmentObject in. Oddly,


1) if I access this value in ContentVIew it crashes... no biggie, I restructured the code a bit, but odd, as I can do it with a separate sample app


2) I have a View accessing this EnvObj. I need to toggle it visible/hidden on screen.

- when I put it in a Group and have a Bool toggle it to show and hide, it access the EnvObj fine. But I lost the handling of Menu Button

- when I put it in a .sheet(), the same code can no longer access the EnvObj! I poke around it it looks like this


(lldb) po self.userSettings

Fatal error: No ObservableObject of type UserSettings found.

A View.environmentObject(_:) for UserSettings may be missing as an ancestor of this view.: file /BuildRoot/Library/Caches/com.apple.xbs/Sources/Monoceros_Sim/Monoceros-39.3/Core/EnvironmentObject.swift, line 55

2019-11-21 09:38:18.555138-0500 TWCAppleTV[35825:1484533] Fatal error: No ObservableObject of type UserSettings found.

A View.environmentObject(_:) for UserSettings may be missing as an ancestor of this view.: file /BuildRoot/Library/Caches/com.apple.xbs/Sources/Monoceros_Sim/Monoceros-39.3/Core/EnvironmentObject.swift, line 55

error: warning: couldn't get required object pointer (substituting NULL): Couldn't load 'self' because its value couldn't be evaluated


error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).

The process has been returned to the state before expression evaluation.

(lldb) po $userSettings

Fatal error: No ObservableObject of type UserSettings found.

A View.environmentObject(_:) for UserSettings may be missing as an ancestor of this view.: file /BuildRoot/Library/Caches/com.apple.xbs/Sources/Monoceros_Sim/Monoceros-39.3/Core/EnvironmentObject.swift, line 55

2019-11-21 09:38:24.548829-0500 TWCAppleTV[35825:1484533] Fatal error: No ObservableObject of type UserSettings found.

A View.environmentObject(_:) for UserSettings may be missing as an ancestor of this view.: file /BuildRoot/Library/Caches/com.apple.xbs/Sources/Monoceros_Sim/Monoceros-39.3/Core/EnvironmentObject.swift, line 55

error: warning: couldn't get required object pointer (substituting NULL): Couldn't load 'self' because its value couldn't be evaluated


error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).

The process has been returned to the state before expression evaluation.

(lldb) po _userSettings

▿ EnvironmentObject<UserSettings>

- _store : nil

- _seed : 1



userSetting is just not there.


Anyone have any insight of what's going on? thx


(This is my first try on SwiftUI, really fun)

Replies

I can't speak to the first issue, as I'm not sure I understand it exactly, but the sheet situation is expected. The sheet represents a whole new stack of views, and much of the environment is not inherited. The solution is to re-attach the environment object there:


VStack {
    // content
}
.sheet(...) {
    MySheetView(...)
        .environmentObject(self.envObj)
}