0

My UIActivityIndicatorView is not showing when I call it before the API request, and will show after the request has been done,

This is my function that run inside the TouchUpInside of my button

func onLogin () {
    let activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
    activityIndicator.center = view.center
    activityIndicator.hidesWhenStopped = true
    activityIndicator.style = UIActivityIndicatorView.Style.gray
    view.addSubview(activityIndicator)

    activityIndicator.startAnimating()

    do {
        let data = try self.getData()
        let loginData = try JSONDecoder().decode(LoginResponse.self, from: data!)
        print(loginData)
    } catch {
        let alert = UIAlertController(title: "Login failed", message: nil, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default))
        present(alert, animated: true, completion: nil)
    }
}

and my request code was

static func getData() throws -> Data? {
    let urlData = URL(string: "www.example.com")
    var request : URLRequest = URLRequest(url: urlData!)
    request.httpMethod = "POST"

    request.httpBody = self.getBodyString.data(using: .utf8)

    var data: Data?
    var response: URLResponse?
    var error: Error?

    let semaphore = DispatchSemaphore(value: 0)

    URLSession.shared.dataTask(with: request) { d,r,e in
        data = d
        response = r
        error = e

        semaphore.signal()
    }.resume()

    _ = semaphore.wait(timeout: .distantFuture)

    if error != nil {
        throw error!
    }

    return data
}

when I remove the do catch with getData() in my onLogin() function the UIActivityIndicatorView was working good.

The response of my API call was request timeout, but I want to see the indicator loading the request.

3
  • Try to use your activity indicator in DispatchQueue.main.async {} Commented Mar 18, 2020 at 7:57
  • @EmreDeğirmenci I didn't put the activityIndicator in the DispatchQueue.main.async {} instead, I wrap my do {} catch {} with it and worked, because when I wrapped my activityIndicator with it, it's still not showing, is that alright? or there's something i missed or not a good practice? Commented Mar 18, 2020 at 17:07
  • I do not have any idea about good practice or not. Commented Mar 18, 2020 at 19:24

1 Answer 1

0

Try to bring activity indictor to front of UIview and Use it in DispatchQueue.main.async

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't put the activityIndicator in the DispatchQueue.main.async {} instead, I wrap my do {} catch {} with it and worked, because when I wrapped my activityIndicator with it, it's still not showing, is that alright? or there's something i missed or not a good practice?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.