1

I use charts for my project. However, I now run into a problem that if a dataset is empty I will all report error when I see debug area.

Now I am trying to find a solution to be able to stop creating the chart if the dataset is empty.

the error message I get 25x :

[32865:1852701] [Unknown process name] CGAffineTransformInvert: singular matrix.

for now I have this solution but I don't know if there is a better one.

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]
}
Wouter
  • 809
  • 7
  • 19

1 Answers1

1

for now I have this solution but I don't know if there is a better one.

I can see from your code that you are trying to get the data each time you open the scene, if so try this approach

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]

}

}

if you are just trying to get them just once as you open the scene i suggest moving those two line

self.lineChartView.noDataText = "Geen data beschikbaar om een grafiek te tekenen."
    getGewichtenHond() 

inside the viewDidLoad method because viewWillAppear will trigger every time you leave the scene and get back to it

Mohmmad S
  • 5,001
  • 4
  • 18
  • 50
  • I try your solution but I can't access context. So I try something whit your solution in mind. The appDelegate and context lets have to be inside a function so far I see and know. Maybe I do or understand something wrong... (I update the question) – Wouter Oct 15 '18 at 10:09
  • you took my solution, didn't ask me how or why, then had a new problem edited the question removed the answer like, idk gl – Mohmmad S Oct 15 '18 at 10:14
  • In that case your are right maybe its a new problem. – Wouter Oct 15 '18 at 10:16
  • for now I have this solution but I don't know if there is a better one. the answer was based on this question that we had before, for this case probably something else, read about DidSet, and willSet. the answer is cleaning the code as you asked for better approach. i hope i helped at some point, if your question is something else, please remove the accepted button might someone can help better – Mohmmad S Oct 15 '18 at 10:16