Skip to content

App crashed every time a value is added #177

Description

@RaphaelLesourd

Keeps on crashing the app , what am I missing here???

this is where it crashes:
internal func setPlotPointPositions(forNewlyActivatedPoints activatedPoints: [Int], withData data: [Double]) {

    var index = 0
    for activatedPointIndex in activatedPoints {
        
        let dataPosition = index
        let value = data[dataPosition]
        
        let newPosition = graphViewDrawingDelegate.calculatePosition(atIndex: activatedPointIndex, value: value)
        graphPoints[activatedPointIndex].x = newPosition.x.      <<<<------ right here
        graphPoints[activatedPointIndex].y = newPosition.y
        
        index += 1
    }
}

error: Thread 1: Fatal error: Index out of range

on initial load , no issues.
every time a child is added to firebase it updates a collection view and the scrollable graph
but it crashes every time:
here is my code:

override func viewDidLoad() {
super.viewDidLoad()

    setupViews()
    dataSource()
    collectionviewSize()
    fetchDatasBmiHistory()
    setupGraph()

}

func dataSource() {
    graph.dataSource = self
    
}

func fetchDatasBmiHistory() {
if shared.currentUser == nil {
return
}

    guard let userID = Auth.auth().currentUser?.uid else { return }
    
    shared.ref_userBMI.child(userID).observe(.childAdded) { (snapshot) in
        if !snapshot.exists() {
            shared.errorCard("Error fetching datas")
            return }
        if let dictionary = snapshot.value as? [String: AnyObject] {
            let user = UserBMI(dictionary: dictionary)
            shared.bmiArray.append(user)
        }
        DispatchQueue.main.async{
            self.collectionView.reloadData()
            self.graph.reload()
        }

}
}

func value(forPlot plot: Plot, atIndex pointIndex: Int) -> Double {
switch(plot.identifier) {
case "bmi":
return shared.bmiArray[pointIndex].bmi ?? 20
default:
return 0
}
}

func label(atIndex pointIndex: Int) -> String {
    return shared.convertTimestamp(serverTimestamp: shared.bmiArray[pointIndex].timestamp ?? 0)
}


func numberOfPoints() -> Int {
        return shared.bmiArray.count
 }

func setupGraph() {
    
    let BMI = LinePlot(identifier: "bmi")
    BMI.lineWidth = 3
    BMI.lineColor = .systemPink
    BMI.lineStyle = ScrollableGraphViewLineStyle.smooth
    BMI.shouldFill = true
    BMI.fillType = ScrollableGraphViewFillType.gradient
    BMI.fillColor = UIColor.systemPink.withAlphaComponent(0.4)
    BMI.fillGradientStartColor = .systemYellow
    BMI.fillGradientEndColor = UIColor.systemPink.withAlphaComponent(0.7)
    BMI.adaptAnimationType = ScrollableGraphViewAnimationType.elastic
   
    let referenceLines = ReferenceLines()
    referenceLines.referenceLineLabelFont = UIFont.boldSystemFont(ofSize: 12)
    referenceLines.referenceLineColor = .quaternaryLabel
    referenceLines.referenceLineLabelColor = .secondaryLabel
    referenceLines.dataPointLabelFont = UIFont.boldSystemFont(ofSize: 11)
    referenceLines.dataPointLabelColor = .white
    referenceLines.shouldAddUnitsToIntermediateReferenceLineLabels = true
    referenceLines.shouldAddLabelsToIntermediateReferenceLines = true
    referenceLines.includeMinMax = true
    referenceLines.referenceLineNumberOfDecimalPlaces = 2
    
    
    let dotPlot = DotPlot(identifier: "darkLineDot")
    dotPlot.dataPointSize = 1
    dotPlot.dataPointFillColor = .secondarySystemFill
    dotPlot.adaptAnimationType = ScrollableGraphViewAnimationType.elastic
    
    
    
    graph.addReferenceLines(referenceLines: referenceLines)
    graph.addPlot(plot: BMI)
    
    
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions