ModelContainer.swift 第 144 行出现致命错误 - 未能找到当前活动的类容器

问题描述 投票:0回答:2

此错误仅出现在预览中 - 我能够正常运行我的应用程序。

我有一个修改表,我想在 SwiftUI 中构建,但预览不起作用,这降低了我的工作效率。当我尝试构建预览时出现此错误:

“未能找到当前活动的习惯容器”

这是我用来填充用于预览的容器的示例目标数据:

struct SampleGoals {
    static var contents: [Goal] =
        [
            Goal(name: "Spanish B2", id: "dasfdsa", habits: [
                Habit(name: "Comprehensible input", id: "dsafda", frequency: .daily),
                Habit(name: "Study Grammar", id: "djsakfdhjska", frequency: .weekly)
             ]),
             Goal(name: "Goal 2", id: "ashdjskaf", habits: [
                Habit(name: "Habit name", id: "dsafe", frequency: .daily)
             ])
        ]
}

这是我的模型:

@Model
class Habit: Identifiable {
    var frequency: Frequency
    var name: String
    var isDone: Bool
    @Attribute(.unique) let id: String

    init(
        name: String,
        id: String,
        frequency: Frequency = .daily,
        isDone: Bool = false
    ) {
        self.name = name
        self.frequency = frequency
        self.isDone = isDone
        self.id = id
    }
}

@Model
class Goal: Identifiable {
    var name: String
    @Attribute(.unique) let id: String
    let habits: [Habit] = [Habit]()

    init(name: String, id: String, habits: [Habit]) {
        self.name = name
        self.habits = habits
        self.id = id
    }
}

Frequency 是一个符合可编码的枚举。

这是我的模型容器:

@MainActor
let previewContainer: ModelContainer = {
    do {
        let config = ModelConfiguration(for: Goal.self, Habit.self, isStoredInMemoryOnly: true)
        let container = try ModelContainer(
            for: Goal.self, Habit.self, configurations: config
        )
        for goal in SampleGoals.contents {
            container.mainContext.insert(goal)
        }
        return container
    } catch {
        fatalError("Failed to create container")
    }
}()

这是我如何应用容器进行预览:

#Preview {
    ModifyGoalView(
        modifyType: .edit,
        modifyGoalPresented: .constant(true),
        goal: SampleGoals.contents[0])
    .modelContainer(previewContainer)
}

我正在使用这个项目来学习 SwiftData,但我不知道为什么会发生这种情况。

我在堆栈溢出和互联网上找到了一些答案,我尝试应用这些答案但没有成功。最初,我没有将习惯模型包含在模型配置的持久模型中,我在 reddit 上找到的一个回复说包含它 - 这没有帮助。我还明确地将模型添加到模型配置中,但这也没有帮助。我已经清理了该项目,将其从模拟器中删除,并删除了我的派生数据。

swiftui swift-data swift-data-modelcontext
2个回答
0
投票

我不断遇到类似的问题,到目前为止我最好的猜测是,当您的预览需要它时,容器尚未准备好。它不会影响预览之外的应用程序,因为您很可能在调用此视图之前很久就创建了容器。

您可以尝试将视图包装在某种容器中,然后将

modelContainer
修饰符应用于该容器吗?

#Preview {
    struct Container: View {
        var body: some View {
            ModifyGoalView(
                modifyType: .edit,
                modifyGoalPresented: .constant(true),
                goal: SampleGoals.contents[0])
        }
    }
    
    return Container().modelContainer(previewContainer)
}

0
投票

我相信 Paul Hudson 在这里提供了您正在寻找的答案:

https://www.hackingwithswift.com/quick-start/swiftdata/how-to-use-swiftdata-in-swiftui-previews

您需要使用 ModelConfiguration 创建一个 ModelContainer 来仅将数据存储在内存中。然后最后创建您的示例数据。

保罗的例子如下:

#Preview {
    let config = ModelConfiguration(isStoredInMemoryOnly: true)
    let container = try! ModelContainer(for: User.self, configurations: config)

    let user = User(name: "Test User")
    return EditingView(user: user)
        .modelContainer(container)
}
© www.soinside.com 2019 - 2024. All rights reserved.