UIAlertController with activity indicator when loading JSON

1

I am loading JSON in my UIViewController in the following way and I need to see a UIAlertController with a Activity Indicator that is automatically hidden when the JSON is fully loaded.

override func viewDidLoad() {

    Alamofire.request(.GET, "http://...com/json/...php?id=\(pasarid)").responseJSON { (responseData) -> Void in
        let swiftyJsonVar = JSON(responseData.result.value!)

        if let resData = swiftyJsonVar["bandas"].array {
            self.completo.text = resData[0]["nombre"].string
            self.historia.text = resData[0]["descripcion"].string
            self.fundacion.text = resData[0]["fun"].string
            self.sede.text = resData[0]["sede"].string
            self.componentes.text = resData[0]["com"].string
            self.director.text = resData[0]["director"].string
            self.procesiones.text = resData[0]["bandas"].string
            self.asisuena.text = resData[0]["marcha"].string

            self.web = resData[0]["web"].string!
            self.facebook = resData[0]["bandas"].string!
            self.twitter = resData[0]["bandas"].string!
            self.correo = resData[0]["bandas"].string!

        }

Thanks in advance

    
asked by Javi Rando 22.02.2016 в 13:43
source

1 answer

1

The activity indicator should show it at the beginning of the viewDidLoad method and hide after getting the JSON in the completion handler of Alamofire.request .

I do not know if the completion handler is running in main thred , if not, then you should do dispatch_async(dispatch_get_main_queue, {...}) to hide it.

Finally, to show a activity indicator you can use for example MBProgressHUD

Example:

override func viewDidLoad() {
    let progressHUD = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
    progressHUD.labelText = "Loading..."
    progressHUD.mode = .Indeterminate

    Alamofire.request(.GET, "http://...com/json/...php?id=\(pasarid)").responseJSON { (responseData) -> Void in
        let swiftyJsonVar = JSON(responseData.result.value!)

        dispatch_async(dispatch_get_main_queue(), {
            MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
        })
        ...
    }
}
    
answered by 22.02.2016 / 13:58
source