0

I am showing my response data in bar chart but the issue is that I am getting response properly and in graph if I pass static data then it works but if I pass my response value then it is not showing. Here is my code

Code::

let usersubcategory = ["user_id": 77 ,"access_token": "f7abee7bffa89898755174dbc6548bd2","account_id": "Mike50430315","year": 2018] as [String : Any]

print(usersubcategory)
Alamofire.request(CallAPI, method: .post, parameters: usersubcategory).responseJSON
    {
        response in

        print(response)
        let result = response.result

        if let dict = result.value as? Dictionary<String,AnyObject>{
            if let categorylist = dict["data"]{

                self.searchlist = categorylist as! [AnyObject]


                 for item in self.searchlist{
                 let value = item["month"]
                 guard let value_chart = value else {
                 continue
                 }
                 let optionalvalue = value_chart
                 if let noLongerOptional = optionalvalue {
                 print("\(noLongerOptional)")
                    let chartConfig = BarsChartConfig(valsAxisConfig: ChartAxisConfig(from: 0, to: 800, by: 100))
                    let frame = CGRect(x: 0, y: 270, width: self.view.frame.width, height: 450)
                    let chart = BarsChart(frame: frame,
                    chartConfig: chartConfig,
                    xTitle: "Months",
                    yTitle: "Count",
                    bars: [
                    ("Jan", noLongerOptional as! Double),
                    ("Feb", noLongerOptional as! Double),
                    ("Mar", noLongerOptional as! Double),
                    ("Apr", noLongerOptional as! Double),
                    ("May", noLongerOptional as! Double),
                    ("Jun", noLongerOptional as! Double),
                    ("July",noLongerOptional as! Double),
                    ("Aug", noLongerOptional as! Double),
                    ("Sep", noLongerOptional as! Double),
                    ("Oct", noLongerOptional as! Double),
                    ("Nov", noLongerOptional as! Double),
                    ("Dec", noLongerOptional as! Double)
                        ],
                                          color: UIColor.darkGray,
                                          barWidth: 15
                    )
                    self.view.addSubview(chart.view)
                    self.chartView = chart
                 }
              }

            }
        }

}

I am unwrapping the value and then passing in charts but when I print no noLongerOptional then I am getting data but not able to pass in graph.

Static value is showing correctly in graph. Can any one please help me?

user28
  • 89
  • 5
  • Put a Xcode breakpoint at your self.chartView = chart, and check which thread your code is running under; pretty sure you are attempting to update your chart from a background thread rather than the main thread. cheers. – ekscrypto Aug 02 '18 at 11:44
  • @ekscrypto sorry but not able to understand what you want to tell me –  Aug 02 '18 at 11:53
  • This is why UIView operations have to be done on the main thread: https://stackoverflow.com/questions/18467114/why-must-uikit-operations-be-performed-on-the-main-thread – ekscrypto Aug 02 '18 at 12:04
  • And this is another user having a similar issue and how he solved it: https://stackoverflow.com/questions/29852431/alamofire-asynchronous-completionhandler-for-json-request – ekscrypto Aug 02 '18 at 12:05
  • How to use Xcode breakpoints: http://jeffreysambells.com/2014/01/14/using-breakpoints-in-xcode – ekscrypto Aug 02 '18 at 12:06
  • @ekscrypto when chart is update at that time all data i get correctly but chart does't update –  Aug 02 '18 at 12:30

1 Answers1

0

Alamofire returns the network response on the networking thread it creates rather than the main thread. You need to dispatch your UIView operations onto the main thread

print(usersubcategory)
Alamofire.request(CallAPI, method: .post, parameters: usersubcategory).responseJSON
    {
        response in

        print(response)
        let result = response.result

        DispatchQueue.main.async {
          if let dict = result.value as? Dictionary<String,AnyObject>{
            if let categorylist = dict["data"]{

                self.searchlist = categorylist as! [AnyObject]


                 for item in self.searchlist{
                 let value = item["month"]
                 guard let value_chart = value else {
                 continue
                 }
                 let optionalvalue = value_chart
                 if let noLongerOptional = optionalvalue {
                 print("\(noLongerOptional)")
                    let chartConfig = BarsChartConfig(valsAxisConfig: ChartAxisConfig(from: 0, to: 800, by: 100))
                    let frame = CGRect(x: 0, y: 270, width: self.view.frame.width, height: 450)
                    let chart = BarsChart(frame: frame,
                    chartConfig: chartConfig,
                    xTitle: "Months",
                    yTitle: "Count",
                    bars: [
                    ("Jan", noLongerOptional as! Double),
                    ("Feb", noLongerOptional as! Double),
                    ("Mar", noLongerOptional as! Double),
                    ("Apr", noLongerOptional as! Double),
                    ("May", noLongerOptional as! Double),
                    ("Jun", noLongerOptional as! Double),
                    ("July",noLongerOptional as! Double),
                    ("Aug", noLongerOptional as! Double),
                    ("Sep", noLongerOptional as! Double),
                    ("Oct", noLongerOptional as! Double),
                    ("Nov", noLongerOptional as! Double),
                    ("Dec", noLongerOptional as! Double)
                        ],
                                          color: UIColor.darkGray,
                                          barWidth: 15
                    )
                    self.view.addSubview(chart.view)
                    self.chartView = chart
                 }
              }

            }
          }
        }
}

This is why UIView operations have to be done on the main thread: Why must UIKit operations be performed on the main thread?

ekscrypto
  • 3,718
  • 1
  • 24
  • 38
  • yes now i can understand whats the issue thanks for help but still my chart is not update as my response data –  Aug 02 '18 at 12:14