Search code examples
swiftcompletionhandlerdispatch-queue

return value from completion handler is not updated in DispatchQueue.main.async block


I am calling a function with completion handler from one class to another class

called class:

class PVClass
{

var avgMonthlyAcKw:Double = 0.0

var jsonString:String!

func estimateMonthlyACkW (areaSqFt:Float, completion: @escaping(Double) -> () ){

var capacityStr:String = ""

let estimatedCapacity = Float(areaSqFt/66.0)
capacityStr = String(format: "%.2f", estimatedCapacity)

// Build some Url string
var urlString:String = "https://developer.nrel.gov/"
urlString.append("&system_capacity=")
urlString.append(capacityStr)

let pvURL = URL(string: urlString)
let dataTask = URLSession.shared.dataTask(with: pvURL!) { data, response, error in
    do {

        let _ = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers)
        self.jsonString = String(data: data!, encoding: .utf8)!
        print("JSON String:\(String(describing: self.jsonString))")

        if self.jsonString != nil {
            let decoder = JSONDecoder()
            let jsonData = try decoder.decode(PVClass.Top.self, from: data!)

            // do some parsing here
            var totalAcKw: Double = 0.0
            let cnt2: Int = (jsonData.Outputs?.ACMonthly.count)!
            for i in 0..<(cnt2-1) {
                totalAcKw = totalAcKw + (jsonData.Outputs?.ACMonthly[i])!
            }
            self.avgMonthlyAcKw = Double(totalAcKw)/Double(cnt2)

            // prints value
            print("updated estimate: ", self.avgMonthlyAcKw)
           completion(self.avgMonthlyAcKw)
        }

    } catch {
        print("error: \(error.localizedDescription)")

    }
}
dataTask.resume()

}

calling class:

  aPVClass.estimateMonthlyACkW(areaSqFt: 100.0, completion: { (monthlyAckW) -> Void in

        DispatchQueue.main.async { [weak self] in
            guard case self = self else {
                return
            }

            print("monthlyAckW: ", monthlyAckW)
            self?.estimatedSolarkWh = Int(monthlyAckW * Double((12)/365 * (self?.numDays)!))
            print("estimatedSolarkWh: ", self?.estimatedSolarkWh ?? 0)
            guard let val = self?.estimatedSolarkWh  else { return }
            print("val: ", val)
            self?.estimatedSolarkWhLabel.text = String(val)
            self?.view.setNeedsDisplay()
        }

    })

 }

monthlyAckW has the right value after completion handler returns. But the assigned value to self?.estimatedSolarkWh is 0, value never gets transferred to the current class scope, UI update fails, even after DispatchQueue.main.async How to fix this please?


Solution

  • The call of completion is at the wrong place. Move it into the completion closure of the data task after the print line

        // prints value
        print("updated estimate: ", self.avgMonthlyAcKw)
        completion(self.avgMonthlyAcKw)
    

    and delete it after resume

    dataTask.resume()
    completion(self.avgMonthlyAcKw)
    }