Код: Выделить всё
func applicationWillTerminate(_ application: UIApplication) {
ChartsService.queryAndHandleData()
}
static func queryAndHandleData() {
// modelContext is create referance in a view onAppear
guard self.modelContext != nil else {
return
}
do {
// the error because this line code
var model = ChartModel()
// query some data
let predicate = Account.predicateAllData()
var descriptor = FetchDescriptor(predicate: predicate)
let accounts = try modelContext?.fetch(descriptor)
guard accounts != nil, accounts!.count > 0 else {
return
}
// handle and save data ....
} catch {
print(error)
}
}
Подробнее здесь: https://stackoverflow.com/questions/785 ... -swiftdata
Мобильная версия