Post

Replies

Boosts

Views

Activity

SwiftData Fatal error: failed to find a currently active container
I'm new to Apple development and decided to learn using SwiftData and Xcode 15 beta 4 (though I'm starting to think I don't need that extra challenge). I've worked through many issues--with you all's help--but this is one I can't figure out and hasn't shown up in my searches. I'm working on the traditional task list app (with a Task model). I've tried several approaches to app structure, but each time I eventually hit this same error. It doesn't happen when I set up the new Xcode project, but eventually it does. I've cleaned the build folder, restarted the Simulator, and even rebooted the Macbook I'm working on. Here's what happens when I click Product > Run: the app builds successfully the Simulator displays a white screen as expected then the error appears. Here are the error messages: In the debug area it says: SwiftData/ModelContainer.swift:159: Fatal error: failed to find a currently active container for Task Failed to find any currently loaded container for Task) and in the warning in the editor says Thread 1: Fatal error: failed to find a currently active container for Task The debugger is highlighting this line in the call stack (not sure if that's useful as a newbie): #7 0x0000000100bb6d90 in Task.init(id:title:priority:) at /var/folders/3v/q8g4z9bx4lb9z6t7mhgwgghw0000gn/T/swift-generated-sources/@__swiftmacro_10BadgerTool4Task5ModelfMm_.swift:2 Here's the code: The Task model isn't complex: // Task.swift import Foundation import SwiftData @Model class Task { @Attribute(.unique) var id: UUID var title: String var priority: String init(id: UUID = UUID(), title: String, priority: String = "None") { self.id = id self.title = title self.priority = priority } } I have the model container set in the context of the highest view: // BadgerToolApp.swift import SwiftUI import SwiftData @main struct BadgerToolApp: App { var body: some Scene { WindowGroup { NavView() .modelContainer(for: Task.self) } } } I tried moving all other views one layer down (which is why the unnecessary NavView is there): // NavView.swift import SwiftUI import SwiftData struct NavView: View { @State var selectedCollection: Collection? @State var selectedTask: Task? var body: some View { NavigationSplitView( sidebar: { SideBarView(selectedCollection: $selectedCollection, selectedTask: $selectedTask) }, content: { ContentListView(selectedCollection: $selectedCollection, selectedTask: $selectedTask) }, detail: { TaskDetailView(selectedCollection: $selectedCollection, selectedTask: $selectedTask) } ) } } Trying to isolate my mistake I removed everything else related to SwiftData (except for the imports) and gave my views hard-coded data for the simulator like this: // SideBarView.swift import SwiftUI import SwiftData struct SideBarView: View { @Binding var selectedCollection: Collection? @Binding var selectedTask: Task? let collections = presetCollections //temp data var body: some View { List(collections, id: \.id, selection: $selectedCollection){ collection in NavigationLink(collection.name, value: collection) } List{ Text("All Tasks") Text("Settings") } .navigationTitle("Collections") } } Everything works as expected if I comment out the .modelContainer() modifier on the call to NavView view I change the Task definition from the @Model class to a regular struct like this: // Task.swift // with @Model removed import Foundation struct Task: Hashable { var id: UUID var title: String var priority: String init(id: UUID = UUID(), title: String, priority: String = "None") { self.id = id self.title = title self.priority = priority } } What am I missing? Could it be a bug? Thanks for your help!
12
4
11k
Jul ’23