Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App crashed every time a value is added #177

Open
RaphaelLesourd opened this issue Apr 17, 2020 · 3 comments
Open

App crashed every time a value is added #177

RaphaelLesourd opened this issue Apr 17, 2020 · 3 comments

Comments

@RaphaelLesourd
Copy link

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)
    
    
}
@benmckillop
Copy link

Hi, I'm having this issue too. I have gotten around it by drawing the graph each time I add a datapoint. However, you need to ensure that you remove the graph view before adding the subview again.

You can do this by adding the following code to the top of the draw graph function.
for view in graphContainer.subviews { view.removeFromSuperview() }

@RaphaelLesourd
Copy link
Author

i see, I have tried that . alas nothing does it... keeps on crashing every single time a new entry is added to firebase data base ... I ll go with something else ... cause it s not viable but thank you

@roarapps
Copy link

@birkyboydesign did you find a good alternative with similar animation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants