我尝试在 Swift 中向我的应用程序添加实时活动。但是,当我尝试运行该应用程序时,出现错误消息“尾随闭包传递给不接受闭包的‘NSManagedObjectContext’类型的参数”。我的应用程序使用 Core Data 来保存我的应用程序的数据。
class HabitManager: ObservableObject {
private var activity: Activity<HabitsWidgetAttributes>? = nil
func startHabit(name: String, symbol: String, currentTask: String) {
let attributes = HabitsWidgetAttributes(name: name, symbol: symbol)
let state = HabitsWidgetAttributes.ContentState(currentTask: currentTask)
activity = try? Activity<HabitsWidgetAttributes>.request(attributes: attributes, contentState: state)
}
func updateActivity(nextTask: String) {
let state = HabitsWidgetAttributes.ContentState(currentTask: nextTask)
Task { // Line where the error is generated
await activity?.update(using: state)
}
}
func stopActivity() {
let state = HabitsWidgetAttributes.ContentState(currentTask: "Finished")
Task { // Line where …Run Code Online (Sandbox Code Playgroud)