Clarification on where to put the Source of Truth

At 30:05 in the video, it says "You can put a source of truth on the view inside of a WindowGroup".

Can I clarify what this means?

Does it mean inside the ContentView() of that WindowGroup, or something else? I ask because it shows WindowGroup > Window > ContentView.

If I'm still targeting iOS 13, and not using WindowGroup, where is the most appropriate place to create a source of truth where I own the lifecycle - maybe the SceneDelegate? (I'm speaking specifically of an ObservableObject that I want one of my tabs to have access to.)

Thanks!

Replies

I think the answer to both of your questions is pretty similar. In iOS 13, I've used the Scene Delegate to hold my source of truth for things like my model. For example, in the willConnectTo function in SceneDelegate, when the ContentView is created, I've simply added in my managed object context (when using Core Data):
Code Block        
let contentView = ContentView().environment(\.managedObjectContext, context)

I've done similar for injecting observable objects into my environment using .environmentObject

In iOS 14 there's no SceneDelegate, but we can do something similar when the ContentView in instantiated.
Code Block    
var body: some Scene {
        WindowGroup {
            ContentView().environment(\.managedObjectContext, context)
    }
}

So I think that's what they mean when they say you can put the source of truth on a view inside a WindowGroup.

Also, I'm not sure I know what you mean by WindowGroup > Window > View. I think the hierarchy is App > Scene > View. WindowGroup is just a primitive version of a scene. https://developer.apple.com/documentation/swiftui/scene