Search code examples
iosswiftrestapialamofire

Is there a way to show Activity Indicator or Progress Bar during response wait from Alamofire api request? Using Swift


Right now I display progress bar that hardcoded to fill in 2 seconds (average response time). But I want it to be intelligent.

My request looks like this.

func makeRequest(to endPoint: String) {
    Alamofire.request(endPoint).responseJSON{ response in
        if let value = response.result.value {
            let responseInJSON = JSON(value)
            self._responseInJSON = responseInJSON
        }
    }
}

Looking forward for your help. Thanks.


Solution

  • I hope it will help i have Indicate in Comment.

    func makeRequest(to endPoint: String) {
    
        // here you can showActivetyIndicator start progressing here
        Alamofire.request(endPoint).responseJSON{ response in
            if let value = response.result.value {
                let responseInJSON = JSON(value)
                self._responseInJSON = responseInJSON
            }
           // here you can hide Your ActivetyIndicator here
        }
    }
    

    Simply you can start progress just before Alamofire request start and stop then after getting API response. no need for static time like 2 Sec.