Search code examples
swift3closuresalamofireuipickerviewunbox

How to save variable in closure to external variable?


I'm trying to create a custom PickerView that gets it's data from an API call to a web-server. The problem I'm having is saving the parsed data into an external variable so that the PickerView protocol methods can access it.

// API Call / Parsing using Alamofire + Unbox
static func makeApiCall(completionHandler: @escaping (CustomDataStructure) -> ()) {
    Alamofire.request(webserverUrl, method: .get).responseObject { (response: DataResponse<Experiment>) in
        switch response.result {
            case .success:
                if var configParams = response.result.value {
                    let inputConfigs = removeExtrasParams(experiment: response.result.value!)
                    let modifiedViewModel = modifyViewModel(experiment: &configParams, inputConfigs: inputConfigs)
                    completionHandler(modifiedViewModel)
                }
            case .failure(_):
                break
        }
    }
}

// Custom PickerClass
class CustomPickerView: UIPickerView {
    fileprivate var customDS: CustomDataStructure?

    override init() {
        super.init()

        dataSource = self
        delegate = self

        SomeClass.makeApiCall(completionHandler: { customds in
            self.customDS = customds
        })
    }

    ...
}

extension CustomPickerView: UIPickerViewDelegate {
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if let customds = customDS {
            if let customDSValues = customds.inputs.first?.value {
                return customDSValues[row]
            }
        }
        return "apple"
    }
}

extension CustomPickerView: UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if let customds = customDS {
            return customds.inputs.values.count
        } else {
            return 0
        }
    }
}

The problem I'm having is that customDS returns nil everytime.

What am I doing wrong here?


Solution

  • In the completion block of makeApiCall simply reload your pickerView's component on main thread and you all set to go.

    SomeClass.makeApiCall(completionHandler: { customds in
        self.customDS = customds
        DispatchQueue.main.async {
            self.reloadComponent(0) 
        }
    })