ivnsch / SwiftCharts

Easy to use and highly customizable charts library for iOS
Apache License 2.0
2.53k stars 410 forks source link

It's seem like can't release chart instance completely? #321

Closed hopy11 closed 6 years ago

hopy11 commented 6 years ago

In my ViewController,I write a method createChart() to create a new chart,below is part of the code:

class ViewController:UIViewController{
    var chart:Chart?

    func createChart(){
        //Omitted code for init layers ...
        let chart = Chart(
            frame: chartFrame,
            innerFrame: innerFrame,
            settings: chartSettings,
            layers: [
                xAxisLayer,
                yAxisLayer,
                guidelinesLayer,
                chartPointsLineLayer,
                chartPointsCircleLayer
            ]
        )

        view.addSubview(chart.view)
        chart.zoom(scaleX: 6, scaleY: 1, centerX: 0, centerY: 0)

        chartPointsLineLayer.initScreenLines(chart)
        chartPointsCircleLayer.initViews(chart)
        chartPointsCircleLayer.zoom(6, y: 1, centerX: 0, centerY: 0)

        chart.delegate = self
        //here set new chart to self.chart
        self.chart = chart
    }
}

In order to refresh the chart's display in time, I call createChart() in viewWillAppear:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    //update some variable data for chart display
    updateData()
    //release(remove) chart from view
    if let chart = chart{
        chart.clearView()
        self.chart = nil
    }
    //recreate chart
    enrollChart()
}

It's mean everytime when user enter the vc,it will first remove chart and then recreate it!

It seem looks good! But actually run the code is bad! it's seem like chart instance not released completely! because the UI of the vc become very slow after few times,and chart display is not right.

my question is : what is the right way for release chart? Is my way correct? thanks :)

hopy11 commented 6 years ago

what I mean : "the UI of the vc become very slow after few times,and chart display is not right." is this:

2017-12-05 10 48 16 2017-12-05 10 48 49 2017-12-05 10 49 06

when I quit App and run it again , the chart display correct! (like the 3th picture above) What's wrong with it? thank you!

ivnsch commented 6 years ago

This usually works

for view in chart.view.subviews {
    view.removeFromSuperview()
}
initChart() // code to create chart instance
chart.view.setNeedsDisplay() // this may be necessary to trigger a redraw

If there's still a problem try profiling, or upload a self containing gist and I'll take a look.

hopy11 commented 6 years ago

OK,it's worked!thanks!