我在项目中使用图表。但是,我现在遇到一个问题,如果数据集为空,当我看到调试区域时,我都会报告错误。
现在,我正在尝试找到一种解决方案,以便在数据集为空时能够停止创建图表。
我得到25倍的错误消息:
[32865:1852701] [未知进程名称] CGAffineTransformInvert: 奇异矩阵。
目前我有此解决方案,但我不知道是否有更好的解决方案。
var gewichtenHond: [GewichtHond] = [] {
didSet {
if self.gewichtenHond.count == 0 {
self.lineChartView.noDataText = "Geen data beschikbaar om een grafiek te tekenen."
} else {
grafiekData(animatieSnelheid: 1.0, typeAnimatie: .easeInBounce)
}
tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
getGewichtenHond()
}
func getGewichtenHond() {
let appDelegate = UIApplication.shared.delegate as? AppDelegate
let context = appDelegate?.persistentContainer.viewContext
let hondFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Hond")
hondFetch.predicate = NSPredicate(format: "hondId = %@", hondId)
let honden = try! context?.fetch(hondFetch)
let hond: Hond = honden?.first as! Hond
self.gewichtenHond = hond.gewichten?.allObjects as! [GewichtHond]
}
答案 0 :(得分:1)
目前我有此解决方案,但我不知道是否有更好的解决方案。
我可以从您的代码中看到,每次打开场景时您都试图获取数据,如果可以,请尝试这种方法
class UIViewController : UIViewController{
var gewichtenHond: [GewichtHond] = [] {
didSet {
grafiekData(animatieSnelheid: 1.0, typeAnimatie: .easeInBounce)
tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
let appDelegate = UIApplication.shared.delegate as? AppDelegate
let context = appDelegate?.persistentContainer.viewContext
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.lineChartView.noDataText = "Geen data beschikbaar om een grafiek te tekenen."
getGewichtenHond()
}
func getGewichtenHond() {
let hondFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Hond")
hondFetch.predicate = NSPredicate(format: "hondId = %@", hondId)
let honden = try! context?.fetch(hondFetch)
let hond: Hond = honden?.first as! Hond
self.gewichtenHond = hond.gewichten?.allObjects as! [GewichtHond]
}
}
如果您只是想在打开场景时让它们一次,我建议您移动 这两行
self.lineChartView.noDataText = "Geen data beschikbaar om een grafiek te tekenen."
getGewichtenHond()
在viewDidLoad
方法内,因为viewWillAppear
会在您每次离开场景并返回场景时触发