Sorry for the rudimentary question, SwiftData question. In the case of Xcode standard sample code,
@main
struct SwiftDataTestProjectsApp: App {
var sharedModelContainer: ModelContainer = {
let schema = Schema([
Department.self,.
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
do {
return try ModelContainer(for: schema, configurations: [modelConfiguration])
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}()
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(sharedModelContainer)
}
}
and create a ModelContainer for the Scene, #Preview {
ContentView()
.modelContainer(for: Item.self, inMemory: true)
}
However, if SwiftData is not used in the ContentView, and only if the transition is made to the other view using NavigationLink, as shown in the code below, a ModelContainer is created and a ModelContainer is specified for the ContentTwoView, If I want to specify a separate container for ContentTwoView, how should I create .modelContainer(sharedModelContainer) and specify .modelContainer(for: Item.self, inMemory: true)? Should I continue to create the ModelContainer for the Scene as before? Or is there a way to create it only for the specified View and specify it there?
struct ContentView: View {
var body: some View {
NavigationStack {
NavigationLink("ContentTwoView") {
ContentTwoView())
}
}
}
}
struct ContentTwoView: View {
@Environment(\.modelContext) private var modelContext
@Query private var items: [Item].
var body: some View {