title: Activity indicator animation not working - Swift
body: <p>I have this problem when I load the screen I start an activity indicator and call a function to get the data. At first the activity was shown but never starts then I ran each call (start activity &amp; get data) in a different thread. The animation worked but only once when I try to load the screen another time it's stuck again until the data is loaded.
  This is the viewDidLoad:</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
  
      //indicator
      indicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
      indicator.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
      indicator.color = commons.getButtonColor()
      indicator.center = self.view.center
      indicator.hidesWhenStopped = true
      self.view.addSubview(indicator)
      DispatchQueue.main.async {
          print("We finished that.")
          // only back on the main thread, may you access UI:
          self.indicator.startAnimating()
      }
  
  
      //connect to database
      commons.ref = Database.database().reference()
  
      //table view
      tableV.dataSource = self
      tableV.delegate = self
      tableV.tableFooterView = UIView()
      tableV.separatorStyle = .none
      tableV.allowsSelection = false
  
      //change left btn
      let leftButtonItem = UIBarButtonItem.init(
          title: "",
          style: .done,
          target: self,
          action: #selector(backButtonPressed)
      )
      leftButtonItem.image = UIImage(named:"back")
      leftButtonItem.tintColor = commons.getButtonColor()
      self.navigationItem.leftBarButtonItem = leftButtonItem
  
      //style
      self.title = flag
  
      DispatchQueue.main.async {
          //get values
          if(self.flag == "Accounts"){
              self.color = "d0f0dd"
              self.getAccounts()
          }else if(self.flag == "Loans"){
              self.color = "f8ff90"
             self.getLoans()
          }else if(self.flag == "Cards"){
              self.color = "ffa669"
              self.getCards()
          }
      }
  
  }
  </code></pre>
  
  <p>A screenshot when the screenis stuck: </p>
  
  <p><a href="https://i.stack.imgur.com/iGbyO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iGbyO.png" alt="enter image description here"></a></p>
  
  <p>Edit: The function to get data:</p>
  
  <pre><code>func getAccounts(){
      commons.ref.child("Accounts").observeSingleEvent(of: .value, with: { (snapshot) in
          for item in snapshot.children {
              self.values.append(item as! DataSnapshot)
          }
          self.tableV.reloadData()
          self.indicator.stopAnimating()
      }) { (error) in
          print(error.localizedDescription)
      }
  }
  
  func getCards(){
      commons.ref.child("Cards").observeSingleEvent(of: .value, with: { (snapshot) in
          self.indicator.stopAnimating()
          for item in snapshot.children {
              self.values.append(item as! DataSnapshot)
          }
          self.tableV.reloadData()
      }) { (error) in
          print(error.localizedDescription)
      }
  }
  
  func getLoans(){
      commons.ref.child("Loans").observeSingleEvent(of: .value, with: { (snapshot) in
          self.indicator.stopAnimating()
          for item in snapshot.children {
              self.values.append(item as! DataSnapshot)
          }
          self.tableV.reloadData()
      }) { (error) in
          print(error.localizedDescription)
      }
  }
  </code></pre>